Get startedGet started for free

Reactivity: simple reactive variable

In reactive programming, an expression gets re-evaluated whenever any of its dependencies are modified. In Shiny, all inputs are reactive variables. This means that any time the user manipulates an input control to change its value, any code block that depends on that variable (such as a render function) reacts to the input variable's new value by re-evaluating.

You can also create new reactive variables with the reactive() function.

The following code defines a reactive variable called my_sum that calculates the sum of two numeric inputs named num1 and num2.

my_sum <- reactive({
  input$num1 + input$num2
})

When does the my_sum variable get re-computed?

This exercise is part of the course

Case Studies: Building Web Applications with Shiny in R

View Course

Hands-on interactive exercise

Turn theory into action with one of our interactive exercises

Start Exercise