Layouts and themes

Building Web Applications with Shiny in R

Kaelen Medeiros

Data Scientist

Default Shiny app layout

ui <- fluidPage(
  titlePanel("Histogram"),
  sliderInput('nb_bins', '# Bins', 5, 10, 5),
  plotOutput('hist')
)
server <- function(input, output, session){
  output$hist <- renderPlot({
    hist(faithful$waiting, 
         breaks = input$nb_bins, 
         col = 'steelblue')
  })
}
shinyApp(ui = ui, server = server)

An app displaying a histogram of waiting times along with a slider input to control number of bins

Building Web Applications with Shiny in R

Sidebar layout

ui <- fluidPage(
  titlePanel("Histogram"),
  sidebarLayout(
    sidebarPanel(sliderInput('nb_bins', 
                             '# Bins', 5, 10, 5)),
    mainPanel(plotOutput('hist'))
  )
)
server <- function(input, output, session){
  output$hist <- renderPlot({
    hist(faithful$waiting, breaks = input$nb_bins, 
         col = 'steelblue')
  })
}
shinyApp(ui = ui, server = server)

An app displaying a histogram of waiting times on the right along with a slider input to control number of bins, on the left

Building Web Applications with Shiny in R

Tab layout

An app displaying a histogram of waiting times on the right along with a slider input to control number of bins, on the left

An app displaying a histogram of waiting times on the right along with a slider input to control number of bins, on the left, with two tabs

Building Web Applications with Shiny in R

Tab layout

ui <- fluidPage(
  titlePanel("Histogram"),
  sidebarLayout(
    sidebarPanel(sliderInput('nb_bins', '# Bins', 
                             5, 10, 5)),
    mainPanel(

tabsetPanel( tabPanel('Waiting', plotOutput('hist_waiting')), tabPanel('Eruptions', plotOutput('hist_eruptions')) )
) ) )
server <- function(input, output, session){
  output$hist_waiting <- renderPlot({
    hist(faithful$waiting, 
         breaks = input$nb_bins, 
         col = 'steelblue')
  })
  output$hist_eruptions <- renderPlot({
    hist(faithful$eruptions, 
         breaks = input$nb_bins, 
         col = 'steelblue')
  })
}
shinyApp(ui = ui, server = server)
Building Web Applications with Shiny in R

Theme selector

ui <- fluidPage(
  titlePanel("Histogram"),

shinythemes::themeSelector(),
sidebarLayout( sidebarPanel(sliderInput('nb_bins', '# Bins', 5, 10, 5)), mainPanel(plotOutput('hist')) ) ) server <- function(input, output, session){ output$hist <- renderPlot({ hist(faithful$waiting, breaks = input$nb_bins, col = 'steelblue') }) } shinyApp(ui = ui, server = server)

An app displaying a histogram of waiting times on the right along with a slider input to control number of bins, as well as a floating theme selector

Building Web Applications with Shiny in R

Adding a theme

ui <- fluidPage(
  titlePanel("Histogram"),

theme = shinythemes::shinytheme('superhero'),
sidebarLayout( sidebarPanel(sliderInput('nb_bins', '# Bins', 5, 10, 5)), mainPanel(plotOutput('hist')) ) ) server <- function(input, output, session){ output$hist <- renderPlot({ hist(faithful$waiting, breaks = input$nb_bins, col = 'steelblue') }) } shinyApp(ui = ui, server = server)

An app displaying a histogram of waiting times on the right along with a slider input to control number of bins, on the left

Building Web Applications with Shiny in R

Let's practice!

Building Web Applications with Shiny in R

Preparing Video For Download...