1. Learn
  2. /
  3. Courses
  4. /
  5. Case Studies: Building Web Applications with Shiny in R

Connected

Exercise

Reactive variables reduce code duplication

In the previous exercises, the code to filter gapminder according to the input values is duplicated three times: once in the table, once in the plot, and once in the download handler.

Reactive variables can be used to reduce code duplication, which is generally a good idea because it makes maintenance easier.

Instructions

100 XP

The duplicated code chunks that filter the data have been removed. Your task is to add a reactive variable that filters the data, and use this variable instead. Specifically:

  • Create a reactive variable named filtered_data by using the reactive() function that uses the filtering code from the previous exercise (line 15).
  • Use the reactive variable to render the table output, the plot output, and the download file (lines 33, 42, and 50).