title: Access a dynamically generated input in r shiny
body: <p>I have an app where the user needs to assign randomly generated elements (in this case, letters) to groups, but gets to decide how many groups to use. Because the <code>selectInput</code> where memberships are defined is generated dynamically in response to a number specified by the user, naming the menu is done automatically (e.g., <code>usergroup1</code>, <code>usergroup2</code>, etc.). I am having trouble accessing the input values and returning them from the module to use later because I won't know in advance how many inputs there will be, and hence how many <code>usergroups</code> to call. Here is an example app:</p>
  
  <p><strong>UI module:</strong></p>
  
  <pre><code>library(shiny) 
  library(stringr)
  
  mod1UI &lt;- function(id) {
    ns &lt;- NS(id)
    tagList(
      numericInput(ns("n"), "N",value = NULL),
      actionButton(ns("draw"),"Generate Letters"),
      hr(),
      numericInput(ns("groups"), "Enter number of groups (1-3)", value=NULL),
      uiOutput(ns("groupings"))
    )
  }
  </code></pre>
  
  <p>What I tried to do here is make a list of <code>usergroup</code> names and return those, but the values aren't attached, and nothing comes through.</p>
  
  <p><strong>Server module:</strong></p>
  
  <pre><code>mod1 &lt;- function(input, output, session, data) {
    ns &lt;- session$ns
    x &lt;- reactiveValues(data=NULL)
  
    observeEvent(input$draw, {
      req(input$n)
      x$data &lt;- sample(letters,input$n)
    })
  
    output$groupings &lt;- renderUI({
      req(input$groups)
      ltrs &lt;- data()
      lapply(1:input$groups, function(i) {
        selectizeInput(paste0(session$ns("usergroup"),i), 
                       paste0("Select letters for Group ", i),
                       choices=ltrs, 
                       options = list(placeholder = "Select letters for this group", 
                                      onInitialize = I('function() { this.setValue(""); }')), multiple=T)
      })
    })
  
    gps &lt;- reactiveValues(gps=NULL)
  
    reactive({
      gps$gps &lt;- lapply(1:input$groups, function(i) { paste0(session$ns("usergroup"),i) })
    })
  
    return(list(dat = reactive({x$data}),
                groups = reactive({gps$gps})
    ))
  }
  </code></pre>
  
  <p><strong>UI:</strong></p>
  
  <pre><code>ui &lt;- navbarPage("Fancy Title",id = "tabs",
           tabPanel("Panel1",
               sidebarPanel(
                    mod1UI("input1")
               ),
               mainPanel(verbatimTextOutput("lettersy")
               )
           )
        )
  </code></pre>
  
  <p><strong>Server:</strong></p>
  
  <pre><code>server &lt;- function(input, output, session) {
    y &lt;- callModule(mod1, "input1", data=y$dat)
    output$lettersy &lt;- renderText({
      as.character(c(y$dat(), y$groups(), "end"))
    })
  }
  
  shinyApp(ui, server)
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>