Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectInput from named list in shiny with single element vectors

Tags:

list

r

shiny

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?

like image 653
markist Avatar asked Jan 23 '18 12:01

markist


1 Answers

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)
        })
    }
)
like image 66
Nate Avatar answered Oct 22 '22 05:10

Nate