闪亮:如何在 selectInput 中获取选项向量

Shiny : How to get vector of options in selectInput

我的 ui.R 文件有这样的 selectInput:

selectInput("cluster", "Cluster:",
                                 c("Total" = "Total","East"="East",                                 
                                   "South"="South", )

其中 "Total" 或 "South" 应该是列名列表的向量。

喜欢 East is : East<-c("Strasbourg","Grenoble","Nice")South is : South<-("Nice","Montpellier","Marseille")

server.r 文件中:

我想做这样的事情:

  resultscluster<-reactive({
   mydataframe[,(names(mydataframe) %in% input$cluster)]

  })

当我 运行 App ui.R 不知道是什么 "cluster".

谢谢

谢谢

对于您的情况,我可能只使用 switch 语句。我还冒昧地添加了一个 validate 语句,要求选择一个选项。

library(shiny)

East <- c("Strasbourg","Grenoble","Nice") 
South <- c("Nice","Montpellier","Marseille")
Total <- c("Strasbourg","Grenoble","Nice",
           "Montpellier","Marseille", "coolPlace1", "coolplace2")

# some play data
mydataframe <- as.data.frame(replicate(7, rnorm(10)))
colnames(mydataframe) <- Total

runApp(
  list(
    ui = pageWithSidebar(
      div(),
      sidebarPanel(
        selectInput("cluster","Cluster:",
                    c("East","South","Total"))),

      mainPanel(
        tableOutput("table"))),

    server = function(input, output){

      resultscluster<-reactive({
        validate(
          need(!is.null(input$cluster),
               "Please select a clutser")
          )

        switch(input$cluster,
          East =  mydataframe[,(names(mydataframe) %in% c("Strasbourg","Grenoble","Nice"))],
          South = mydataframe[,(names(mydataframe) %in% c("Nice","Montpellier","Marseille"))],
          Total = mydataframe[,(names(mydataframe) %in% c("Strasbourg","Grenoble","Nice",
                                                          "Montpellier","Marseille", "coolPlace1", "coolplace2"))],
        )

      })

      output$table <- renderTable(resultscluster())
    }
    ))