【问题标题】:SelectInput from named list in shiny with single element vectorsSelectInput 从命名列表中闪亮的单元素向量
【发布时间】:2018-01-23 12:52:42
【问题描述】:

所以,我正在尝试从命名列表中进行选择,这在遵循 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

有人知道如何规避这个问题吗?

【问题讨论】:

    标签: r list shiny


    【解决方案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)
            })
        }
    )
    

    【讨论】:

    • 这在使用服务器端选择时不起作用。
    【解决方案2】:

    @Nate 的回答不适用于服务器端选择。

    例如,使用相同的示例但将选项移动到服务器不会保留命名向量信息:

    shinyApp(
      ui = fluidPage(
        selectizeInput("state", "Choose a state:", NULL),
        textOutput("result")
      ),
      server = function(input, output, session) {
        updateSelectizeInput(
          session = session,
          inputId = "state",
          choices =        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")),
          selected = NULL,
          server = TRUE
        )
        output$result <- renderText({
          paste("You chose", input$state)
        })
      }
    )
    

    shinyWidgets::pickerInput() 按预期工作。我无法仅使用 shiny 找到解决方案。

    library(shinyWidgets)
    shinyApp(
      ui = fluidPage(
        pickerInput("state", "Choose a state:", NULL),
        textOutput("result")
      ),
      server = function(input, output, session) {
        updatePickerInput(
          session = session,
          inputId = "state",
          choices =        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")),
          selected = NULL
        )
        output$result <- renderText({
          paste("You chose", input$state)
        })
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 2018-10-17
      • 2015-05-07
      • 2016-10-08
      • 2014-01-20
      • 2016-01-24
      • 1970-01-01
      • 2019-05-25
      • 2021-09-23
      相关资源
      最近更新 更多