0

I am new to asking questions on Stack Overflow, forgive me in advance. I am trying to create a dynamic number of input elements that run a module. The most similar solution to my problem could be found in this answer:

https://stackoverflow.com/a/19131027/3018211[1]

Code copied below:

ui.R
----

shinyUI( pageWithSideBar(
    ...
    selectInput("numIndividuals", ...)
    uiOutput("sliders"),
    ...
))

server.R
--------

shinyServer( function(input, output, session) {

  output$sliders <- renderUI({
    numIndividuals <- as.integer(input$numIndividuals)
    lapply(1:numIndividuals, function(i) {
      sliderInput(...)
    })
  })


})

I want to change sliderInput(...) to call a slider module:

#slider module ------------------------
sliderUI <- function(id) {
  ns <- NS(id)
  sliderInput(ns("bins"), "Number of Bins:", min = 1, max = 5, value = 3)
}

slider <- function(input, output, session) {}

Any advice would be great! Thanks!

Update: I did a poor job of asking the question, thanks for the comments. I figured out how to run a module within the renderUI context above. What I'm struggling with now is how to store the values output by the module which are the user input values. This code dynamically creates a specified number of number inputs based on user selection. When the user changes the value of each numericInput, I want to store that value in a list. Here is an example (runs, doesn't do what I want in marked places below):

library(shiny)

#module
NumberUI <- function(id){
  ns <- NS(id)
  numericInput(ns("obs"), "Observations:", 10, min = 1, max = 100)
}

Number <- function(input, output, session){}

ui <- shinyUI(fluidPage(
  titlePanel("Test"),

  sidebarLayout(
    sidebarPanel(
      numericInput("numInputs", "How many inputs do you want", 4),

      # place to hold dynamic inputs
      uiOutput("inputGroup")
    ),

# this is just a demo to show the input values, I want to be able to update     
    mainPanel(
      textOutput("inputValues")
    )
  )
))

server <- shinyServer(function(input, output) {

  # observe changes in "numInputs", and create corresponding number of inputs
  observeEvent(input$numInputs, {

    LL <- vector("list", input$numInputs)
    output$inputGroup = renderUI({
      input_list <- lapply(1:input$numInputs, function(i) {
        inputName <- paste0("input", i)
        LL[[i]]<- list(NumberUI(inputName)) 
     })
    })

      #run module
      lapply(1:input$numInputs, function(i) {
      inputName <- paste0("input", i)
      callModule(Number, inputName)
    })
  })

  # this is to display all the input values which I want, this doesn't work
  output$inputValues <- renderPrint({
    paste(lapply(1:input$numInputs, function(i) {
      inputName <- paste0("input", i)
      input[[inputName]]
    }))
  })

})

shinyApp(ui, server)
Community
  • 1
  • 1
  • Welcome to SO. Please include a minimal example explaining what doesn't work – HubertL Feb 27 '17 at 21:08
  • So where exactly are you getting stuck? What's the problem? I assume you've seem the [presentation on creating modules](https://github.com/rstudio/ShinyDeveloperConference/blob/master/Modules/01-Modules.pdf)? There's one almost exactly like this there. Just replace `sliderInput()` with `sliderUI()` – MrFlick Feb 27 '17 at 21:09
  • I reposted this question with a working example of what I'm looking for that does not contain a module and a similar broken example to the one above. I reopened it as a new question because I'm not sure the way I asked this question really captured what I'm looking for. Check it out, any and all suggestions appreciated! [new question](http://stackoverflow.com/q/42633078/3018211) – user3018211 Mar 06 '17 at 18:57
  • Does `inputName <- paste0("input", i, "-obs")` work for you? – Gregor de Cillia Oct 06 '17 at 19:43

0 Answers0