So, i'm trying to select from a named list, which works nicely when following the selectInput reference page:
## Only run examples in interactive R sessions
if (interactive()) {
# basic example
shinyApp(
ui = fluidPage(
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)
# demoing optgroup support in the `choices` arg
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY", "NJ", "CT"),
`West Coast` = c("WA", "OR", "CA"),
`Midwest` = c("MN", "WI", "IA"))
),
textOutput("result")
),
server = function(input, output) {
output$result <- renderText({
paste("You chose", input$state)
})
}
)
}
Standard
However, if i use a list containing named vectors with single elements in it, this somehow changes the selector organisation, only showing the list name but not what it contains for those single element vectors.
## Only run examples in interactive R sessions
if (interactive()) {
# basic example
shinyApp(
ui = fluidPage(
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)
# demoing optgroup support in the `choices` arg
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY"),
`West Coast` = c("WA", "OR", "CA"),
'North Coast' = c("canada"),
`Midwest` = c("MN", "WI", "IA"),
'south coast' = c("Argentina")
)
),
textOutput("result")
),
server = function(input, output) {
output$result <- renderText({
paste("You chose", input$state)
})
}
)
}
Altered list
Anyone ideas how to circumvent this?
If you use a named vector for the single element list items it will render the groups and sub-groups. Live app here.
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY" = "ny"),
`West Coast` = c("WA", "OR", "CA"),
`North Coast` = c("Canada" ="canada"),
`Midwest` = c("MN", "WI", "IA"),
`south coast` = c("Argentina" = "argentina")
)
),
textOutput("result")
),
server = function(input, output) {
output$result <- renderText({
paste("You chose", input$state)
})
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With