title: Generate observers for dynamic number of inputs
body: <p>I have what I think is a pretty simple user case that I haven't been able to find a solution for: I want Shiny to generate a user-specified number of inputs, dynamically creating an observer for each.</p>
  
  <p>In the minimal reproducible code below, the user indicates the number of action buttons desired by typing into the <code>textInput</code> widget; he or she then presses "submit", which generates the action buttons. </p>
  
  <p>What I want is for the user to then be able to click on any action button and generate an output specific to it (e.g. for the minimal case, just print the name of the button):</p>
  
  <pre><code>library("shiny")
  
  ui &lt;- fluidPage(textInput("numButtons", "Number of buttons to generate"), 
                  actionButton("go", "Submit"), uiOutput("ui"))
  
  server &lt;- function(input, output) {
  
          makeObservers &lt;- reactive({
  
                  lapply(1:(as.numeric(input$numButtons)), function (x) {
  
                          observeEvent(input[[paste0("add_", x)]], {
  
                                  print(paste0("add_", x))
  
                          })
  
                  }) 
          })
  
          observeEvent(input$go, {
  
                  output$ui &lt;- renderUI({
  
                          num &lt;- as.numeric(isolate(input$numButtons))
  
                          rows &lt;- lapply(1:num, function (x) {
  
                                  actionButton(inputId = paste0("add_", x), 
                                           label = paste0("add_", x))
  
                          })
  
                          do.call(fluidRow, rows)
  
                  })
  
                  makeObservers()
  
          })
  
  
  }
  
  shinyApp(ui, server)
  </code></pre>
  
  <p>The problem with the code above is that somehow several observers are created, but they all take as their input only the last item in the list passed to <code>lapply</code>. So if I generate four action buttons, and I click on action button #4, Shiny prints its name four times, while all the other buttons don't react.</p>
  
  <p>The idea to generate observers using <code>lapply</code> comes from <a href="https://github.com/rstudio/shiny/issues/167#issuecomment-152598096" rel="noreferrer">https://github.com/rstudio/shiny/issues/167#issuecomment-152598096</a></p>