简体   繁体   中英

Shiny : How to get vector of options in selectInput

My ui.R file has a selectInput like this:

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

where "Total" or "South" are supposed to be a vector of colnames list.

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

like in server.r file :

i would like to do something like that :

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

  })

When I run the App the ui.R does not know what "cluster".

thanks

Thanks

In your case, I would probably just use a switch statement. I also took the liberty of adding a validate statement requiring an option to be selected.

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())
    }
    ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM