16.2 Combine reactive values
A reactive graph shows the connections between the reactive components of the app, which are directional indicating the direction of reactivity; when a connection is not in use, the graph highlights it in grey color which means that the connection is invalidated.
In this first part we learn how to combine reactive values which are escaping the graph and to do this we need to:
1 - modify the value of a reactive value
2 - use reactiveVal() or reactiveValues() functions
3 - combine them with observe() and observeEvent() in the server
In particular, we will see how invalidation caused by the user might not be captured by the reactivity graph, and for this reason, it is said to be escaping the graph. An example is a request of changing the input made by the user, such as when using the reset button (made with an actionbutton()), which is not evidenced in the reactivity graph.
The figure below shows an app with an actionButton(), the input value of the app changes but the reactivity graph stays the same. The reactive graph does not capture the connection between the unnamed observer and the input.
reactlog_enable()
ui <- fluidPage(
textInput("name", "Your name here"),
actionButton("clr", "Clear"),
textOutput("salutation")
)
server <- function(input, output, session) {
# reactive function
hello <- reactive(paste0("Hello ", input$name))
output$salutation <- renderText(hello())
# observer
observeEvent(input$clr, {
updateTextInput(session, "name", value = "")
})
}
# shinyApp(ui = ui, server = server)
To have an idea of what is happening inside the app we can sketch a graph of the connections with one of the {diagrammeR} functions for making flowcharts, such as the mermaid() function, so, we visually identify the connections related to the action of the reset button.
Figure 16.2 Graph of the App
In this contest, the reactive graph represent a static connection that doesn’t show the recursivity of the reset. Another way to visualize and/or modify interactively the reactive graph is to change it directly, moving the parts aside and adjusting it as it is needed.