Observers vs. reactives

Building Web Applications with Shiny in R

Ramnath Vaidyanathan

VP of Product Research

Reactive flow

A diagram displaying reactive flow in a Shiny app

Building Web Applications with Shiny in R

Observers (1/2)

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

Building Web Applications with Shiny in R

Observers (2/2)

ui <- fluidPage(
  textInput('name', 'Enter your name')
)

server <- function(input, output, session){
  observe({
    showNotification(
      paste("You entered the name", input$name)
    )
  })
}
Building Web Applications with Shiny in R

Observers vs. reactives

Role

  • reactive() is for calculating values, without side effects.
  • observe() is for performing actions, with side effects.

Differences

  • Return Values: Reactive expressions return values, but observers don’t.
  • Evaluation: Observers eagerly respond to changes in their dependencies, while reactive expressions are lazy.
  • Side Effects: Observers are primarily useful for their side effects, wherease, reactive expressions must NOT have side effects
Building Web Applications with Shiny in R

Let's practice!

Building Web Applications with Shiny in R

Preparing Video For Download...