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

时间:2015-02-17 09:01:25

标签: r shiny

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

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

其中“Total”或“South”应该是一个colnames列表的向量。

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

就像在server.r档案中一样:

我想做类似的事情:

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

  })

当我运行应用程序时,ui.R不知道什么是“群集”。

感谢

由于

1 个答案:

答案 0 :(得分:3)

在您的情况下,我可能只会使用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())
    }
    ))