How to create a UI in Shiny from a for loop whose length is based on numeric input?

后端 未结 1 1979
慢半拍i
慢半拍i 2020-12-20 10:20

As an extension of this example:

https://shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html

Say you would like the for loop to be of a length determine

相关标签:
1条回答
  • 2020-12-20 10:42

    This should do the trick, as per @Dean, yes the second renderUI shouldn't be there

    library(shiny)
    
    ui <- fluidPage(
      title = 'Creating a UI from a dynamic loop length',
      sidebarLayout(
        sidebarPanel(
          # Determine Length of Loop
          numericInput(inputId = "NumLoop", "Number of Loops", value = 5, min = 1, max = 10, step = 1)
        ),
        mainPanel(
          # UI output
          uiOutput('moreControls')
        )
      )
    )
    
    server <- function(input, output, session) {
    
      output$moreControls <- renderUI({
        lapply(1:input$NumLoop, function(i) {
          strong(paste0('Hi, this is output B#', i),br())
        })
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    0 讨论(0)
提交回复
热议问题