在 RShiny ui 中,如何根据您的选择动态显示多个 numericInput

In RShiny ui, how to dynamic show several numericInput based on what you choose

我的代码在这里:

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")))

))

server.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 })

})

这是一个非常简单的案例,ui 就像:

我期望的是,如果我 select "Choice 2",ui 会给我这个:

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

你必须在服务器端渲染它

例子

显示 1 ,2 和 3 输入基于 select

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-- 两个输入 e.t.c

更新

硬代码示例

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 })
  
})