首页 > 解决方案 > 如何仅使用列表名称创建 selectInput?(不显示列表内容)闪亮

问题描述

我有 3 个向量:

colors = c("blue", "red", "green", "yellow", "white", "black")
numbers = c("1", "2", "3", "4")
things = c("phone", "fridge", "computer", "chair", "table", "notebook", "bag", "jacket")

为了将它们全部列在一个列表中,我将它们加入到一个列表中。

list_options = list("colors" = colors, "numbers" = numbers, "things" = things)

我的主要目标是创建一个闪亮的应用程序,它的 SelectInput 仅包含列表名称(“颜色”、“数字”、“事物”)。但是,如果您单击其中一个选项,您将能够获得列表的值以便以后使用它们。

例如,如果您选择“事物”,我想查看向量内的所有变量(电话、冰箱、计算机……)。

有没有办法做到这一点?现在,使用我的代码,该应用程序会显示每个向量的名称及其选项,并为您提供您选择的选项。

图片

这就是我希望在应用程序中看到我的 selectInput 的方式。

图2

但是,正如我所说,我想稍后使用每个向量的内容(在我的示例中,我编写了一个“renderText”)。如果您选择“颜色”,您将看到“蓝色、红色、绿色、黄色……”。

这是我的代码:

colors = c("blue", "red", "green", "yellow", "white", "black")
numbers = c("1", "2", "3", "4")
things = c("phone", "fridge", "computer", "chair", "table", "notebook", "bag", "jacket")
    
list_options = list("colors" = colors, "numbers" = numbers, "things" = things)


if (interactive()) {
        
  shinyApp(
    ui = fluidPage(
      selectInput("option", "Options:", list_options),
      textOutput("text")
    ),
    server = function(input, output) {
      output$text <- renderPrint({
        input$option
      })
    }
  )
}

首先十分感谢,

标签: rlistshinyshinyappsselectinput

解决方案


我不确定是否理解。是不是你想要的:

  shinyApp(
    ui = fluidPage(
      selectInput("option", "Options:", names(list_options)),
      textOutput("text")
    ),
    server = function(input, output) {
      output$text <- renderPrint({
        list_options[[input$option]]
      })
    }
  )

推荐阅读