Monday, January 23, 2017

Update input in module with dynamic number of ui elements

Leave a Comment

I want to write a module with a dynamic number n, say, of UI elements. I observed that the input slot does not correspond with current number of elements but the largest number chosen during the session:

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) 

If you change the number of elements in the numeric input field from 3 to 2, we see that there are still 3 elements in input. Is this behaviour intentional? If so, how can I update the input to asure that it only contains valid slots?

Update

Here are the 2 relevant screenshots:

3 Products selected

2 Products selected

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment