Reactivity 101

Building Web Applications with Shiny in R

Ramnath Vaidyanathan

VP of Product Research

An app where the user can enter their name, and in response get a personalized greeting

Building Web Applications with Shiny in R

Reactive source

User input that comes through a browser interface, typically

ui <- fluidPage(
  titlePanel('Greeting'),

textInput('name', 'Enter Name')
) server <- function(input, output, session){ } shinyApp(ui = ui, server = server)
Building Web Applications with Shiny in R

Reactive endpoint

Output that typically appears in the browser window, such as a plot or a table of values

ui <- fluidPage(
  titlePanel('Greeting'),
  textInput('name', 'Enter Name'),
  textOutput('greeting')
)

server <- function(input, output, session){
output$greeting <- renderText({ paste("Hello", input$name) })
}
Building Web Applications with Shiny in R

Reactive conductor

An intermediate that depends on reactive sources, and/or updates reactive endpoints.

server <- function(input, output, session){
  output$plot_trendy_names <- plotly::renderPlotly({
    babynames %>% 
      filter(name == input$name) %>%
      ggplot(val_bnames, aes(x = year, y = n)) +
      geom_col() 
  })
  output$table_trendy_names <- DT::renderDT({
     babynames %>% 
      filter(name == input$name)
  })
}
Building Web Applications with Shiny in R

Reactive expressions

Reactive expressions are lazy and cached.

server <- function(input, output, session){
  rval_babynames <- reactive({
    babynames %>% 
      filter(name == input$name)
  })
  output$plot_trendy_names <- plotly::renderPlotly({
     rval_babynames() %>%
      ggplot(val_bnames, aes(x = year, y = n)) +
      geom_col() 
  })
  output$table_trendy_names <- DT::renderDT({
     rval_babynames()
  })
}
Building Web Applications with Shiny in R

Let's practice!

Building Web Applications with Shiny in R

Preparing Video For Download...