Introduction to Shiny

Laura Biggins

02/12/2020

R packages required

Shiny Cheatsheet

https://shiny.rstudio.com/images/shiny-cheatsheet.pdf

Example Shiny app

A live version of this app can be accessed here

Another example app

UI and Server

There are two parts to a Shiny application

Skeleton Shiny code



library(shiny)

ui <- fluidPage(
  
)

server <- function(input, output, session) {
  
}

shinyApp(ui, server)

Simple app code

library(shiny)

ui <- fluidPage(
  
  titlePanel("Simple app"),
  
  radioButtons(inputId = "letter_choice", 
              label    = "select a letter", 
              choices  = LETTERS[1:5]),
  numericInput(inputId = "number_choice",
               label   = "type in a number",
               value   = 4),
  textOutput(outputId = "message")
)

server <- function(input, output) {
  
  output$message <- renderText({
    paste0("You selected the letter ", input$letter_choice, 
          " and the number ", input$number_choice)
  })
}

shinyApp(ui, server)

UI code


ui <- fluidPage(
  
  titlePanel("Simple app"),
  
  radioButtons(inputId = "letter_choice", 
              label    = "select a letter", 
              choices  = LETTERS[1:5]),
  
  numericInput(inputId = "number_choice",
               label   = "type in a number",
               value   = 4),
  
  textOutput(outputId = "message")
)

Inputs

ui <- fluidPage(  
  radioButtons(inputId = "letter_choice", label = "choose a letter", ...),
  numericInput(inputId = "number_choice", label = "", ...)
)
server <- function(input, output) {
  ...
}  

Inputs

ui <- fluidPage(  
  radioButtons(inputId = "letter_choice", ...),
  numericInput(inputId = "number_choice", ...)
)
server <- function(input, output) {
  ...
}  

The input object is a special type of named list.
From within the server code, the values of the input object can be accessed in the same way as list values.

input$letter_choice
"B"


input[["letter_choice"]]
"B"
input$number_choice
4


input[["number_choice"]]
4

Server code

Accessing and using the input values

input$letter_choice
"B"
input$number_choice
4


server <- function(input, output {
  
  output$message <- renderText({
    
    paste0("You selected the letter ", input$letter_choice, 
          " and the number ", input$number_choice)
  })
}

Outputs

ui <- fluidPage(  
  ...,
  textOutput(outputId = "message")
)

Output in the simple app

Output function in UI code pairs with a render function in the server code.
Within the render function are instructions telling Shiny what to show the user.

ui <- fluidPage(...,
  textOutput(outputId = "message")
)
server <- function(input, output, session) {
  output$message <- renderText({
    paste0("You selected the letter ", input$letter_choice, 
           " and the number ", input$number_choice)
  })
}  

Recap

library(shiny)
ui <- fluidPage(
  
  titlePanel("Simple app"),
  radioButtons(inputId = "letter_choice", 
              label    = "select a letter", 
              choices  = LETTERS[1:5]),
  numericInput(inputId = "number_choice",
               label   = "type in a number",
               value   = 4),
  textOutput(outputId = "message")
)

server <- function(input, output, session) {
  
  output$message <- renderText({
    
    paste0("You selected the letter ", input$letter_choice, 
          " and the number ", input$number_choice)
  })
}
shinyApp(ui, server)

Running a shiny application


Inserting code snippet