Reactive programming

Reactive programming combines features of solutions we’ve seen so far.

library(shiny)
reactiveConsole(TRUE) # let's us use reactivity in console

temp_c <- reactiveVal(10) # create
temp_c()                  # get
#> [1] 10
temp_c(20)                # set
temp_c()                  # get
#> [1] 20


temp_f <- reactive({
  message("Converting") 
  (temp_c() * 9 / 5) + 32
})
temp_f()
#> Converting
#> [1] 68

# temp_f automatically updates
temp_c(-10)
temp_f()
#> Converting
#> [1] 14

# _and_ only computes when needed (don't see "Converting")
temp_f()
#> [1] 14