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)