Question

I am trying to run a shiny application that has a dynamic number of inputs. I achieved that based on previous stack overflow discussions, but now I would like to set it so that the starting value for each of the inputs is different.

Selection of UI.R syntax:

sidebarPanel(
selectInput("n", "Number of Test Scores Available", choices = c(1,2,3,4), selected = 1),
uiOutput("dyn_input")

Selection of server.R syntax:

output$dyn_input <- renderUI({
inputs <- lapply(1:input$n, function(i) {
input_name  <- paste("Test", i, sep="")
input_score <- paste("Score", i, sep="")
wellPanel(      
selectInput(
input=  input_name,
label= "Test Name", choices=c("Fall NWF"="FallNWF",
"Fall ORF"="FallORF",
"Spring NWF"="SpringNWF",
"Spring ORF"="SpringORF"
)),
sliderInput(input_score,"Score:",
min=0, max=250,value=0))
})
do.call(tagList, inputs)
}) 

When the user selects multiple tests I would like the default value for Test2 to be FallORF, Test3 to be SpringNWF and Test4 to be SpringORF, right now they all default to FallNWF. Thanks for your help. If you need more information to help me feel free to ask.

Was it helpful?

Solution

Looks good, you're almost there, you just need to set the selected attribute in the selectInput calls. Since you already have the index i of the default you want to select for each dynamic selectInput, you could achieve this for example like this (choices moved to a variable and choices[i] used for the selected attribute):

output$dyn_input <- renderUI({
    inputs <- lapply(1:input$n, function(i) {
        input_name  <- paste("Test", i, sep="")
        input_score <- paste("Score", i, sep="")
        choices <- c("Fall NWF"="FallNWF",
                     "Fall ORF"="FallORF",
                     "Spring NWF"="SpringNWF",
                     "Spring ORF"="SpringORF")
        wellPanel(      
            selectInput(
                input=  input_name,
                label= "Test Name", choices=choices, selected = choices[i]),
            sliderInput(input_score,"Score:",
                        min=0, max=250,value=0))
    })
    do.call(tagList, inputs)
}) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top