我的代码在这里:
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 })
})
我的期望是,如果我选择&#34;选择2&#34;,ui会给我这个:
那么我怎么能达到我的期望?
答案 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 })
})