Get Started

Trigger reactions with observeEvent()

There are times when you want to perform an action in response to an event. For example, you might want to let the app user download a table as a CSV file, when they click on a "Download" button. Or, you might want to display a notification or modal dialog, in response to a click.

The observeEvent() function allows you to achieve this. It accepts two arguments:

  1. The event you want to respond to.
  2. The function that should be called whenever the event occurs.

In this exercise, you will use observeEvent() to display a modal dialog with help text, when the user clicks on a button labelled "Help". The help text has already been assigned to the variable bmi_help_text.

This is a part of the course

“Building Web Applications with Shiny in R”

View Course

Exercise instructions

  • UI:
    • Add an action button named 'show_help', labelled "Help". You might have to scroll down or collapse the console to view the UI.
  • Server:
    • Uncomment the code # showModal ...
    • Wrap showModal(...) in observeEvent() so that the help text is displayed when a user clicks on the help button

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

ui <- fluidPage(
  titlePanel('BMI Calculator'),
  sidebarLayout(
    sidebarPanel(
      textInput('name', 'Enter your name'),
      numericInput('height', 'Enter your height in meters', 1.5, 1, 2),
      numericInput('weight', 'Enter your weight in Kilograms', 60, 45, 120),
      actionButton("show_bmi", "Show BMI")
      # CODE BELOW: Add an action button named "show_help"
      
    ),
    mainPanel(
      textOutput("bmi")
    )
  )
)

server <- function(input, output, session) {
  # MODIFY CODE BELOW: Wrap in observeEvent() so the help text 
  # is displayed when a user clicks on the Help button.
  
     # Display a modal dialog with bmi_help_text
     # MODIFY CODE BELOW: Uncomment code
     # showModal(modalDialog(bmi_help_text))
  
  rv_bmi <- eventReactive(input$show_bmi, {
    input$weight/(input$height^2)
  })
  output$bmi <- renderText({
    bmi <- rv_bmi()
    paste("Hi", input$name, ". Your BMI is", round(bmi, 1))
  })
}

shinyApp(ui = ui, server = server)
Edit and Run Code

This exercise is part of the course

Building Web Applications with Shiny in R

IntermediateSkill Level
4.8+
14 reviews

Shiny is an R package that makes it easy to build interactive web apps directly in R, allowing your team to explore your data as dashboards or visualizations.

In this chapter, you will learn about reactive programming. You will learn about reactive sources, conductors and endpoints and how they come together to drive the magic behind Shiny. You will also learn how to utilize your understanding of reactivity to build performant Shiny apps.

Exercise 1: Reactivity 101Exercise 2: Source vs. Conductor vs. EndpointExercise 3: Add a reactive expressionExercise 4: Understanding reactive expressionsExercise 5: Observers vs. reactivesExercise 6: Add another reactive expressionExercise 7: Does this have a side effect?Exercise 8: Add an observer to display notificationsExercise 9: Stop - delay - triggerExercise 10: Stop reactions with isolate()Exercise 11: Delay reactions with eventReactive()Exercise 12: Trigger reactions with observeEvent()
Exercise 13: Controlling action triggersExercise 14: Applying reactivity conceptsExercise 15: Reactivity concepts: observe & reactiveExercise 16: Convert height from inches to centimeters

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free