Interface Builder Functions
Customizing UI
Understanding how to build a user interface
Earlier in the course we mentioned that the UI is ultimately built with HTML. We actually use R functions to make the UI, but they get translated to HTML.
In this module we build on this idea to develop more complex app interfaces. More specifically, we discuss tags.
tag -> HTML
For example, let’s use the b
tag, which is used for bolding text in HTML. The function is tags$b()
.
You can pass a text string, in quotation marks, to this function, like “This is my first app”.
$b("This is my first app") tags
Then R translates the text string to HTML:
<b>This is my first app</b>
The HTML is then rendered as bolded text when the user sees it:
This is my first app
First level heading
Second level heading
Third level heading
Linked text
If you have references you want to link to at the bottom of you app, you can use the a tag with the href
argument for specifying a URL.
library(shiny)
library(bslib)
# Define UI with tags
<- page_fluid(
ui $h1("Awesome title"),
tags$br(), # line break
tags$a("This app is built with Shiny.", href = "https://shiny.posit.co/")
tags
)
# Define server fn that does nothing :)
<- function(input, output, session) {}
server
# Create the app object
shinyApp(ui = ui, server = server)
Awesome title
These are functions like:
a()
for anchor text.$a("Anchor text") tags
<a>Anchor text</a>
a("Anchor text")
<a>Anchor text</a>
br()
for a line break.$br() tags
<br>
br()
<br>
code()
for displaying code in monospace form$code("Monospace text") tags
<code>Monospace text</code>
code("Monospace text")
<code>Monospace text</code>
And the heading functions we mentioned earlier
$h1("First level header") tags
<h1>First level header</h1>
h1("First level header")
<h1>First level header</h1>
The function names correspond to the tag names, and the functions accept text strings as arguments. For example tags$h1("First level heading")
is equivalent to h1("First level heading")
.
HTML
If you’re comfortable with HTML, an alternative is to directly use HTML syntax and wrap your HTML code with the HTML function.
HTML("Hello world, <br/> and then a line break.")
<br/> and then a line break. Hello world,
Practice
Next, let’s work on some exercises on adding HTML elements to our apps to customize appearance.
These next exercises will demo a few of the simpler interface builder options with HTML code.
Practice - Add image with the img
tag
Your turn
Now let’s practice adding an image to a very simple app.
Note that if you’d like to use a local image for with your app, you’ll first have to save the image in a folder
www/
within your root directory. You’ll see what we mean when you run the demo in the Posit Cloud Project. Set a height and width for your image as well.
Navigate to the Posit Cloud Project titled 4-1b Add image with img tag in your Posit Cloud Workspace for the demo
# Load packages ----------------------------------------------------------------
library(shiny)
library(bslib)
# Define UI --------------------------------------------------------------------
<- page_fluid(
ui titlePanel("An image"),
$img(___),
tags
)
# Define server ----------------------------------------------------------------
<- function(input, output, session) {}
server
# Create the Shiny app ---------------------------------------------------------
shinyApp(ui = ui, server = server)
# Load packages ----------------------------------------------------------------
library(shiny)
library(bslib)
# Define UI --------------------------------------------------------------------
<- page_fluid(
ui titlePanel("An image"),
$img(height = 100, width = 300, src = "roles_implement.png"),
tags
)
# Define server ----------------------------------------------------------------
<- function(input, output, session) {}
server
# Create the Shiny app ---------------------------------------------------------
shinyApp(ui = ui, server = server)