Last active
May 19, 2023 22:16
-
-
Save mikmart/31eac2805e9670c0ac0c37652831221b to your computer and use it in GitHub Desktop.
Handle nested observers in Shiny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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