Set up auto-update/isolate logic for reactive contexts
Source:R/parse_utils.R
setup_auto_update_logic.RdA helper function that encapsulates the common pattern of handling auto-update functionality in module servers. When auto-update is disabled, it adds a dependency on the update button. Returns a wrapper function that either isolates reactive expressions or passes them through unchanged.
Arguments
- input
The Shiny input object from the module server, should have both
auto.update(boolean) andupdate(button) inputs.- params
Optional reactive-defaults store from
setup_reactive_defaults(), orNULL. When supplied, aninput$<key>read whosekeyis backed by the store resolves from the store instead of the client input, so a parameter driven by the parent app updates in a single render.- track
Optional function, or
NULL. When supplied, it is called with the id of everyinput$<key>read through the returned wrapper, so a caller can tell which inputs an expression depends on (the modules' cached plot step keys its cache on them).
Value
A function that wraps reactive expressions. With params and track
both NULL this is identity if auto-update is enabled (expressions will be
reactive), or isolate if auto-update is disabled (expressions will not
trigger reactivity). Otherwise it is a wrapper with the same isolation
semantics that additionally redirects store-backed input$<key> reads and
reports the ids read to track.
Details
This function consolidates the following common pattern:
auto_update <- input$auto.update
if (!auto_update) {
input$update
}
isolate_fn <- if (auto_update) identity else isolateUsage in a reactive context:
Examples
if (interactive()) {
library(shiny)
library(plotly)
ui <- fluidPage(
viz_select_input("x_var", "X variable", choices = names(mtcars), selected = "wt"),
viz_select_input("y_var", "Y variable", choices = names(mtcars), selected = "mpg"),
checkboxInput("auto.update", "Auto-update", value = TRUE),
actionButton("update", "Update"),
plotlyOutput("myPlot")
)
server <- function(input, output, session) {
output$myPlot <- renderPlotly({
isolate_fn <- setup_auto_update_logic(input)
x_val <- isolate_fn(input$x_var)
y_val <- isolate_fn(input$y_var)
plot_ly(mtcars, x = ~ .data[[x_val]], y = ~ .data[[y_val]], type = "scatter",
mode = "markers")
})
}
shinyApp(ui, server)
}