selectInput的动态数 [英] dynamic number of selectInput

查看:68
本文介绍了selectInput的动态数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,所以这可能是一个非常基本的问题。
我想编写一个闪亮的应用程序,用户在其中输入 n,我们得到n个selectInput选项,但无法执行。基本上任何形式的for循环都不起作用。
我尝试执行的代码是

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)


推荐答案

您需要将创建的输入存储在列表中并返回该列表,也可以将语句包装在 lapply ,而不是 。下面给出一个工作示例,希望这会有所帮助!

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)

这篇关于selectInput的动态数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆