arevaju
arevaju

Reputation: 163

selectInput box in R/shiny - choices list with same label for multiple values

I am developing a Web App using rShiny (shiny_0.9.1) R version 3.1.0 (2014-04-10) and Platform: x86_64-w64-mingw32/x64 (64-bit). I am using the selectInput method in my ui file for selecting two columns ("IND1_WATER", "IND2_WATER") from a dataframe. Here is the working example code:

  selectInput(inputId = "indicators",
                       multiple = TRUE,
                       label = "Select the list of indicators",
                       choices = list("Water stress" = "IND1_WATER",
                                      "Water scarcity" = "IND2_WATER")

However I would like to label the two columns with the same name and use those for rendering a chart. I have tried different options like "Water stress" = c("IND1_WATER","IND2_WATER") or "Water stress" = colnames(mydf)[6:7] but they didn't work. It only takes the first value. Any ideas on how to solve this problem?

Juan

Upvotes: 2

Views: 3827

Answers (4)

david glen
david glen

Reputation: 77

Adding to this issue as

indicators <- strsplit(input$indicators, ",")[[1]]

will issue a warning: "longer object length is not a multiple of shorter object length" when used in conjunction with:

filter_at(., vars(col1, col2), any_vars(. == indicators))

You must use:

filter_at(., vars(col1, col2), any_vars(. %in% indicators))

Upvotes: 0

arevaju
arevaju

Reputation: 163

Here is the answer to my last question. In order to have multiple values returned for a single selection the ui file should be something like this:

  selectInput(inputId = "indicator",
                label = (HTML("<b>Select a list of indicators:</b>")),
                multiple = TRUE,
                selected = 'Water stress',
               choices = list("Water stress" = "Water stress", "Water scarcity" = "Water scarcity" ))

While the server file should include some conditional statements in order to be able to select more than one column one a particular value is selected:

passData <- reactive({
    data <- data[data$Country %in% input$country_filter1 & 
                   data$Year %in% input$year,]
    rownames(data) = unique(data$NUTS_CODE)
    if (input$indicator== 'Water stress') {col= 7:9}
    if (input$indicator== 'Water scarcity') {col= 8:9}
    data[,col]
  }) 

I hope it helps others with a similar problem. Thanks to my colleague Javier Martinez for his invaluable contribution!

Upvotes: 0

Yihui Xie
Yihui Xie

Reputation: 30114

With the latest version of shiny on CRAN (>= 0.10.1), you can use the new optgroup feature (see an example here). In your case,

selectInput(inputId = "indicators",
            multiple = TRUE,
            label = "Select the list of indicators",
            choices = list("Water stress" = c("IND1_WATER", "IND2_WATER")))

So please update.packages(ask = FALSE) if you have not updated your R packages.

Upvotes: 2

MrFlick
MrFlick

Reputation: 206177

Each "choice" can only have one value associated with it. If you want to have multiple values returned for a single selection, you'll have to fake it by concatenating the values you want. For example

selectInput(inputId = "indicators",
    multiple = TRUE,
    label = "Select the list of indicators",
    choices = list("Water stress" = "IND1_WATER,IND2_WATER")

And then on the server side

indicators <- strsplit(input$indicators, ",")[[1]]

Which will work if there are commas or not.

Upvotes: 2

Related Questions