Reputation: 331
This is based on a previous question asked about R Shiny, but my goal is each time the user selects something different from the dropdown, different sliders appear.
Simply put, if the user selects Level A, then sliders AA, BB, and CC appear. If they select Level B, then sliders DD and EE appear. Lastly if they select Level C then FF, GG, HH, II appear.
My code is below, but I am not sure how to add multiple sliders. Any help is appreciated!
ui <- fluidPage(
sidebarLayout(position = "left",
sidebarPanel(
selectInput("level",
"Level:",
choices = ''),
uiOutput("new"),
width=2),
mainPanel()
))
server = shinyServer(function(input, output, session) {
observe({
updateSelectInput(session, inputId = "level", label = "Level:", choices = c('A', 'B', 'C'))
})
output$new <- renderUI({
if (!input$level == 'A') return(NULL) else {
sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10)
#sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10)
#sliderInput("slider_cc", "CC:", min = 0, max = 90, value = 10)
}
})
})
shinyApp(ui,server)
EDIT: I found a solution, in part thanks to comments below!!!!
ui <- fluidPage(
sidebarLayout(position = "left",
sidebarPanel(
selectInput("level",
"Level:",
choices = ''),
uiOutput("new"),
uiOutput("new1"),
uiOutput("new2"),
width=2),
mainPanel()
))
server = shinyServer(function(input, output, session) {
observe({
updateSelectInput(session, inputId = "level", label = "Level:", choices = c('A', 'B', 'C'))
})
output$new <- renderUI({
#tagList(sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10), sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10))
if (!input$level == 'A') return(NULL) else {
tagList(sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10),
sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10))
}
})
output$new1 <- renderUI({
#tagList(sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10), sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10))
if (!input$level == 'B') return(NULL) else {
tagList(sliderInput("slider_cc", "CC:", min = 0, max = 90, value = 10),
sliderInput("slider_dd", "DD:", min = 0, max = 90, value = 10))
}
})
output$new2 <- renderUI({
#tagList(sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10), sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10))
if (!input$level == 'C') return(NULL) else {
tagList(sliderInput("slider_ee", "EE:", min = 0, max = 90, value = 10))
}
})
})
shinyApp(ui,server)```
Upvotes: 1
Views: 62
Reputation: 325
You can add all in one single renderUI
ui <- fluidPage(
sidebarLayout(position = "left",
sidebarPanel(
selectInput("level",
"Level:",
choices = ''),
uiOutput("new"),
width=2),
mainPanel()
))
server = shinyServer(function(input, output, session) {
observe({
updateSelectInput(session, inputId = "level", label = "Level:", choices = c('A', 'B', 'C'))
})
output$new <- renderUI({
#tagList(sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10), sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10))
if (input$level == 'A') {
tagList(sliderInput("slider_aa", "AA:", min = 0, max = 90, value = 10),
sliderInput("slider_bb", "BB:", min = 0, max = 90, value = 10))
} else if(input$level == 'B'){
tagList(sliderInput("slider_cc", "CC:", min = 0, max = 90, value = 10),
sliderInput("slider_dd", "DD:", min = 0, max = 90, value = 10))
} else if(input$level == 'C'){
tagList(sliderInput("slider_ee", "EE:", min = 0, max = 90, value = 10))
}
})
})
shinyApp(ui,server)
Upvotes: 2