dynamic number of selectInput

孤街醉人 提交于 2019-12-12 19:26:42

问题


I am new to shiny so this might be a very basic question. I want to write a shiny app where the user inputs 'n' and we get n number of selectInput options and am not able to do it. Basically any form of for loop is not working. The code I attempted is following

library(shiny)
ui = fluidPage(

sidebarLayout(
sidebarPanel(
  textInput(inputId = "number", label = "number of selectInput",value = 5)





),
mainPanel(
  uiOutput(outputId = "putselect")
)
)
)
server = function(input,output){

  output$putselect = renderUI(
    if(input$number != 0 ){
      for(i in 1:(input$number)){

        selectInput(inputId = "i", label = "just write something", choices   = c(2,(3)))
  }
}

  )
}
shinyApp(ui = ui , server = server)

回答1:


You either need to store the inputs you create in a list and return that list, or you can simply wrap your statement in lapply instead of for. A working example is given below, hope this helps!

library(shiny)
ui = fluidPage(

  sidebarLayout(
    sidebarPanel(
      textInput(inputId = "number", label = "number of selectInput",value = 5)
    ),
    mainPanel(
      uiOutput(outputId = "putselect")
    )
  )
)
server = function(input,output){

  output$putselect = renderUI(
    if(input$number != 0 ){
      lapply(1:(input$number), function(i){
        selectInput(inputId = "i", label = paste0("input ",i), choices   = c(2,(3)))
      })
    }
  )
}
shinyApp(ui = ui , server = server)


来源:https://stackoverflow.com/questions/51700437/dynamic-number-of-selectinput

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