However, you must tell R to run the expresssion for this to happen because R uses a style of execution known as lazy evaluation. It looks up the current value of each object that it uses and computes new ouput. Think of it like this: every time you run an expression, the expression updates itself. a <- 1 print ( a ) # 1 a <- 2 print ( a ) # 2 This isn’t reactivity it’s just standard R behavior. Everything in R updates itself each time it is run. Updating an out of date expression is not hard: you just need to re-run the expression. For example, at the end of this code, the expression print(a) is out of date. Reactivity ensures that the output of print(input$a) is always up to date, but what does it mean for output to be out of date? Let’s consider output – and the expression that made it – to be out of date if one of the objects in the expression has been given a new value since the expression was called.
R expressions update themselves, if you ask R expressions update themselves, if you askġ.We’ll look at each of them (and the process itself), with a simple thought experiment: how could we recreate our basic app without breaking the rules of R? I’ve prepared four maxims to help you understand this process. The reality is that Shiny is re-running your R expressions in a carefully scheduled way. The illusion is that information is being pushed from inputs to outputs (or at least that inputs and outputs are linked in an inseperable way). Think of reactivity as a magic trick: reactivity creates the illusion that one thing is happening, when in fact something else is going on. Reactivity appears to reverse the flow of information in R.
Incredibly, this isn’t what happens, as you saw above. a is just sitting passively in memory.įor our app, this suggests that R should look up the value of input$a once, print the value, and then not notice when input$a changes. It is telling R to look up the value of a. This is to imply that the expression on the right is doing the work. Notice that the arrow in the diagram goes from right to left. a <- 1 a + 1 # 2 a <- 2 # (nothing happens) Let’s take a look at reactivity by building a very simple Shiny app: # app.R library ( shiny ) ui <- fluidPage ( headerPanel ( "basic app" ), sidebarPanel ( sliderInput ( "a", label = "Select an input to display", min = 0, max = 100, value = 50 ) ), mainPanel ( h1 ( textOutput ( "text" ))) ) server <- function ( input, output ) if you later change the value of a. avoid the errors that come from misusing reactive values in R (which is easy to do!).create more efficient and sophisticated Shiny apps, and.You don’t need to know how reactivity occurs to use it (just follow the steps laid out in Lesson 4), but understanding reactivity will make you a better Shiny programmer. It lets the app instantly update itself whenever the user makes a change. Reactivity is what makes your Shiny apps responsive.