【问题标题】:In RShiny ui, how to dynamic show several numericInput based on what you choose在 R Shiny ui 中,如何根据您选择的内容动态显示多个数字输入
【发布时间】:2016-08-26 14:22:06
【问题描述】:

我的代码在这里:

ui.R

shinyUI(fluidPage(

  # Copy the line below to make a select box 
  selectInput("select", label = h3("Select box"), 
              choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
              selected = 1),
  numericInput("value",label="value", value=100),

  hr(),
  fluidRow(column(3, verbatimTextOutput("value")))

))

服务器.R

server=shinyServer(function(input, output) {
output$inputs=renderUI({
if(input$select =="1"){
  numericInput(inputId = paste0("value",1),"1",100)

} else if(input$select=="2"){
    numericInput(inputId ="value","value",100),
    numericInput(inputId ="value","value",200),
    numericInput(inputId ="value","value",300)
  }

})

# You can access the value of the widget with input$select, e.g.
output$value <- renderPrint({ input$select })

})

这是一个非常简单的案例,用户界面如下:

我期望的是,如果我选择“选择 2”,ui 会给我这个:

那么我怎样才能达到我的期望呢?

【问题讨论】:

    标签: shiny shiny-server shinydashboard


    【解决方案1】:

    你必须在服务器端渲染它

    示例

    根据选择显示 1 ,2 和 3 输入

    library(shiny)
    ui=shinyUI(fluidPage(
      
      # Copy the line below to make a select box 
      selectInput("select", label = h3("Select box"), 
                  choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
                  selected = 1),
      uiOutput("inputs"),
      
      hr(),
      fluidRow(column(3, verbatimTextOutput("value")))
      
    ))
    
    
    server=shinyServer(function(input, output) {
      output$inputs=renderUI({
        lapply(1:input$select,function(i){
          numericInput(inputId = paste0("value",i),paste0("value",i),100)
        })
      })
      
      # You can access the value of the widget with input$select, e.g.
      output$value <- renderPrint({ input$select })
      
    })
    
    shinyApp(ui,server)
    

    我使用简单的逻辑:如果您选择 1,那么一个输入会被重新设计,2-- 两个输入等

    更新

    硬代码示例

    server=shinyServer(function(input, output) {
      output$inputs=renderUI({
        if(input$select==1){
          numericInput(inputId = paste0("value1"),paste0("value1"),100)
        }else if( input$select==2){
          list(
            numericInput(inputId = paste0("value1"),paste0("value1"),100),
            numericInput(inputId = paste0("value2"),paste0("value2"),200),
            numericInput(inputId = paste0("value3"),paste0("value3"),500)
            
          )
        }else if (input$select==3){
          numericInput(inputId = paste0("value1"),paste0("value1"),100)
        }
        
      })
      
      # You can access the value of the widget with input$select, e.g.
      output$value <- renderPrint({ input$select })
      
    })
    

    【讨论】:

    • @实际上我想要当我选择选项 1 然后选择一个输入,选择 2 然后选择 3 输入,然后选择 3 然后只有 1 个输入。我知道 lapply 在您的示例中很有用,但我不确定在我的情况下是否有简单的方法。此外,当我有选择 2 的三个输入时,我希望初始值不同。例如三个输入的 100.200,500。
    • 我试图达到我的期望,但失败了。我在原文中更新的代码不起作用。
    • 您可以通过多种方式进行操作,例如硬编码(参见更新)
    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2019-11-29
    • 1970-01-01
    • 2022-01-02
    • 2019-08-21
    相关资源
    最近更新 更多