Fine tune the reactivity
1. Fine tune the reactivity
Let's review some of the basics of shiny reactivity.2. Reactivity review
Reactive variables, such as inputs or variables created with the reactive() function, are different from regular variables because they can automatically trigger any code that depends on them to re-evaluate when they're modified. What does it mean that code depends on a reactive variable? By simply accessing a reactive variable, it becomes a dependency for the code that calls it. For example, in this code that defines a variable x, there are three values that are used: input$num1, input$num2 and a reactive variable y. The code to evaluate x will get re-run every time y or one of the inputs is modified.3. Isolate
This behavior can be controlled with the isolate function. Isolating a chunk of code means that anything within this code will not create a reactive dependency. In other words, usually when any reactive value changes then the entire block of code is re-run, but if any reactive value that is inside an isolate is modified then it does not cause the code to re-run. You can think of isolate as a way to escape the automatic triggers of reactivity. In this code, input$num1 has now been isolated, so now if input$num1 changes, nothing will happen. Only when y or input$num2 change, x will get re-evaluated. You can also isolate more than a single variable at a time, you can put any code inside an isolate function. Here, the multiplication of input$num1 with input$num2 has been isolated. The result is that now x will only get re-evaluated when y is updated, but not when any of the inputs are.4. Isolate everything
What if instead of re-evaluating x when one of its reactive dependencies are updated, you want the user to decide when to update x? In that case, you would want to isolate all the reactive variables, so that nothing happens when any reactive variables are updated. But there would still be the problem of how to execute the x reactive code on demand.5. Action buttons
This is where action buttons can help. Action buttons (or, more simply, just buttons) are considered inputs - they have an inputId and a label like all other inputs. But they behave a little differently from other inputs. Most inputs provide the user with some widget that they can change its value, and that value becomes accessible in the server. Buttons have only one possible interaction: the user can click on it. Since a button doesn't have an inherent value, the value of the button's input is an integer. Every time a button gets clicked, its value is incremented by one.6. Action buttons as reactivity triggers
This means that buttons can be useful to trigger reactivity. How? Well, by merely accessing the value of a button, with input$button-id, the button becomes a reactive dependency. To continue our running example of calculating x, in order to use a button to control the reactivity, the first step would be to add a button to the UI. Let's give it an id of calculate-underscore-x. Then in the code that calculates x, input-dollar-sign-calculate-x needs to be used outside of an isolate. This input value doesn't have to be part of any expression, it can just be on its own line. This is enough to make it a dependency. Now whenever the button gets clicked, its value is incremented, which will cause x to recalculate.7. Let's practice!
Now that you've learned some more advanced reactivity topics, it's time to put the finishing touches on your app.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.