r - 使用单元素向量从命名列表中选择输入

标签 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

任何人的想法如何规避这个?

最佳答案

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

关于r - 使用单元素向量从命名列表中选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48402259/

相关文章:

r - 根据R中的匹配条件组合行中的值

R:Ruby中有at_exit这样的函数吗?

flutter - 如何使用 getx 包在 Flutter 中使列表可观察?

python - 如何将二进制数字列表变成点分十进制数字列表?

R Shiny App 在 R Shiny Pro 中与数据库断开连接

r - 观察事件有两个条件

r - 润滑mdy功能

使用条件将值替换为上一行

java - 如何将带有键和值的树形图按升序排列?

r - 是否可以有条件地在 Shiny 的应用程序中显示控制栏?