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

蓝咒 提交于 2020-07-01 16:51:17

问题


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 determined by a numeric input. So for example, extending the linked example (using just the second part of it):

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 = 5, step = 1)
    ),
    mainPanel(
      # UI output
      lapply(1:input.NumLoop, function(i) {
        uiOutput(paste0('b', i))
      })
    )
  )
)

server <- function(input, output, session) {
  reactive({
    lapply(1:input$NumLoop, function(i) {
      output[[paste0('b', i)]] <- renderUI({
        strong(paste0('Hi, this is output B#', i))
      })
    })
  })
}

shinyApp(ui = ui, server = server)

As far as I can tell there are two problems with the code:

In the UI, I don't know how to legitimately use the input from NumLoop in the for loop of the UI output. I have experimented with the conditionalPanel function with no luck.

In the server, once I put the loop behind a reactive function to make use of input$NumLoop I no longer have access to those renderUI outputs in the UI.

Any ideas of how to solves these issues would be much appreciated.


回答1:


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)



来源:https://stackoverflow.com/questions/52847043/how-to-create-a-ui-in-shiny-from-a-for-loop-whose-length-is-based-on-numeric-inp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!