使用动态数量的ui元素更新模块中的输入

时间:2017-01-13 15:25:14

标签: r shiny

我想编写一个动态数字 n 的模块,比如UI元素。我发现input广告位与当前的元素数量相对应,但是会话期间选择的最大数量:

library(shiny)
library(plyr)

testUI <- function(id) {
  ns <- NS(id)
  uiOutput(ns("container"))
}

test <- function(input, output, session, numElems) {
   output$container <- renderUI(do.call(tagList, llply(1:numElems(), function(i) 
            selectInput(session$ns(paste0("elem", i)), 
                        label = i, choices = LETTERS[sample(26, 3)]))))
   getNames <- reactive(reactiveValuesToList(input))
   list(getNames = getNames)
}

ui <- fluidPage(numericInput("n", "Number of Elems", value = 3), 
                testUI("column1"), 
                verbatimTextOutput("debug"))

server <- function(input, output, session) {
   getN <- reactive(input$n)
   handler <- callModule(test, "column1", getN)
   output$debug <- renderPrint(handler$getNames())
}

shinyApp(ui, server)

如果您将数字输入字段中的元素数量从3更改为2,我们会发现3中仍有input个元素。这种行为是故意的吗?如果是这样,我如何更新input以确保它只包含有效的插槽?

更新

以下是2个相关的屏幕截图:

3 Products selected

2 Products selected

0 个答案:

没有答案
相关问题