Dynamically create observers for a shiny app

I am looking for a way to dynamically create actionBttns along with observers in a shiny app. Creating the actionBttns is easy enough using lapply(). I am having trouble creating the observers for each button, as they all should do slightly different things (call the same function, but with different inputs)

here is a minimal working example. I commented my non-working attempt to create the observers with lapply, but it calls the function, before the input list is generated:

Warning: Error in : object 'input' not found
44: <observer>
1: runApp

numberOfButtons <- 6

ui <- dashboardPage(
  dashboardHeader(title = "test"),
      actionButton("nextBtn", "open something"),
      lapply(1:numberOfButtons, function(x){
        actionBttn(inputId = paste0("button_", x), label = paste0("button_", x))  

server <- function(input, output, session) {
  observeEvent(input[["nextBtn"]], {
      title = "testing something",
      easyClose = TRUE
  # lapply(1:numberOfButtons, function(x){
  #   observe(input[[paste0("button_", x)]],{
  #     showModal(modalDialog(
  #       title = as.character(x),
  #       paste(x,"!"),
  #       easyClose = TRUE
  #     ))
  #   })
  # })


shinyApp(ui, server)

You need actionButton in the first lapply and observeEvent in the second one.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.