Skip to content

Instantly share code, notes, and snippets.

@mikmart
Last active May 19, 2023 22:16
Show Gist options
  • Save mikmart/31eac2805e9670c0ac0c37652831221b to your computer and use it in GitHub Desktop.
Save mikmart/31eac2805e9670c0ac0c37652831221b to your computer and use it in GitHub Desktop.
Handle nested observers in Shiny
library(shiny)
ui <- fluidPage(
sliderInput("a", "Slider A", 0, 10, 1),
sliderInput("b", "Slider B", 0, 10, 1),
actionButton("create_observers", "Create observers")
)
server <- function(input, output, session) {
slider_observers <- list()
observeEvent(input$create_observers, {
# Nested observers would be duplicated if not tracked and destroyed.
# Comment out the line below and click "Create" repeatedly to see that happen.
lapply(slider_observers, function(observer) observer$destroy())
slider_observers$a <<- observe(message("Slider A has value ", input$a))
slider_observers$b <<- observe(message("Slider B has value ", input$b))
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment