我很想在Stack Overflow上提问,请提前原谅我。我正在尝试创建一个运行模块的动态数量的输入元素。我的问题最类似的解决方案可以在这个答案中找到:
https://stackoverflow.com/a/19131027/3018211[1]
下面复制的代码:
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(...)
})
})
})
我想更改sliderInput(...)来调用滑块模块:
#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) {}
任何建议都会很棒!谢谢!
更新:我提出这个问题做得不好,谢谢你的评论。我想出了如何在上面的renderUI上下文中运行模块。我现在正在努力的是如何存储模块输出的值,这些值是用户输入值。此代码根据用户选择动态创建指定数量的数字输入。当用户更改每个numericInput的值时,我想将该值存储在列表中。这是一个例子(运行,不在我下面标记的地方做我想要的):
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)