The following code does not work as intended, because only the observer for the third button has any effect and prints the output of both of the Show buttons. How can it be modified to work ? Could an example similar to this be added to the bottom of the help page to help less expert users ? If I don't use variables, then it works as expected.
library(shiny)
observerList <- list()
server <- shinyServer(function(input, output)
{
observeEvent(input[["add"]], {
addToButton <- "listen1"
observerList <<- observeEvent(input[[addToButton]], print(2))
addToButton <- "listen2"
observerList <<- c(observerList, observeEvent(input[[addToButton]], print(3)))
})
})
ui <- fluidPage(
actionButton("add", "Add Observers"),
actionButton("listen1", "Show 2"), # Nothing printed, but 2 should be.
actionButton("listen2", "Show 3") # 2 and 3 printed, although only 3 should be printed.
)
shinyApp(ui = ui, server = server)