使用单个元素向量从带有名称的列表中选择输入

时间:2018-01-23 12:52:43

标签: r list shiny

所以,我正在尝试从命名列表中进行选择,该列表在跟随selectInput参考页面时效果很好:

## 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

但是,如果我使用包含带有单个元素的命名向量的列表,则会以某种方式更改选择器组织,仅显示列表名称,但不显示它们包含的单个元素向量。

## 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

任何想法如何绕过这个?

1 个答案:

答案 0 :(得分:1)

如果对单个元素列表项使用命名向量,它将呈现组和子组。实时应用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)
        })
    }
)