0

My code is here:

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

})

This is a very simple case and the ui is like: enter image description here

What I expect is that if I select "Choice 2", ui would give me this: enter image description here

So how I can achieve my expectation?

Bratt Swan
  • 1,068
  • 3
  • 16
  • 28

1 Answers1

2

You have to render it on server side

Example

Show 1 ,2 and 3 input based on 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)

There i use simple logic : if your choise 1 so one input redered, 2-- two inputs e.t.c

Update

Hard code example

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 })
  
})
Community
  • 1
  • 1
Batanichek
  • 7,761
  • 31
  • 49
  • @Actually I want when I select choice 1 then one input, choice 2 then 3 input, and choice 3 then only 1 input. I know lapply is useful in your example but i am not sure is there an easy way in my case. Also, when I have three inputs for choice 2, I'd like initial values to be different. Such as 100.200,500 for three inputs. – Bratt Swan Aug 25 '16 at 19:15
  • I tried to achieve my expectation but failed. The code I updated in my original text doesn't work. – Bratt Swan Aug 25 '16 at 19:56
  • You can do it in many ways for example hard code it (see update) – Batanichek Aug 26 '16 at 06:21