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

时间:2016-08-25 02:40:41

标签: shiny shiny-server shinydashboard

我的代码在这里:

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就像: enter image description here

我的期望是,如果我选择&#34;选择2&#34;,ui会给我这个: enter image description here

那么我怎么能达到我的期望?

1 个答案:

答案 0 :(得分: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 - 两个输入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 })

})