-2

I have a selectInput() for European countries... in a drop down menu, where the user can click it and chose which country he wishes to see. now what I want to do is, that i want to add three more options to the scroll down menu called "All Europe", "Central Europe" and "Non Central Europe".

I could do that only as a placeholder inside the Selectinput function... but what I want is, that when a user chooses Non-Central Europe, he gets output of the European countries that are in the considered as non central. and I have the identifications. means , i know what countries are considiered non-central (regarding my company's management) ... and the Central Europe category would include All but the Non-central. and All Europe category will contain all the 11 countries..

any suggestions plz ?

all options in one drop-down menu

and also to clear out.. I have a dataset with a lot of variables, one variable is called "Office COuntry", and I want that this variable to be somehow (donno how) related to the region! ,and at any moment the user chooses "Non_Central" for example ..then he directly gets the information about all the Noncentral countries... like to make it easier for the user and faster and more agile .. instead of choosing each country manually

Aurèle
  • 11,334
  • 1
  • 29
  • 47
  • Even though this question is now answered, here is some general advice for better question asking: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Aurèle Jul 24 '18 at 15:20

1 Answers1

0
library(shiny)

countries <- list(
  "Central" = list(
    "Country A",
    "Country B"
  ),
  "Non central" = list(
    "Country C",
    "Country D"
  )
)

ui <- fluidPage(
  selectInput("sel_region", "Select regions", multiple = TRUE,
              choices = names(countries)),
  selectInput("sel_country", "Select countries", multiple = TRUE,
              choices = countries)
)

server <- function(input, output, session) {

  observe({
    x <- unlist(countries[input$sel_region])
    updateSelectInput(session, "sel_country", selected = x)
  })

}

shinyApp(ui, server)

Edit (per OP's comment):

With just one selectInput():

library(shiny)

countries <- list(
  "Central" = list(
    "Country A",
    "Country B"
  ),
  "Non central" = list(
    "Country C",
    "Country D"
  )
)

ui <- fluidPage(
  selectInput("sel_country", "Select countries", multiple = TRUE,
              choices = c(list("Regions" = c("All", names(countries))), countries))
)

server <- function(input, output, session) {

  observe({
    if ("All" %in% input$sel_country) {
      updateSelectInput(session, "sel_country", selected = unlist(countries))
    } else if (any(input$sel_country %in% names(countries))) {
      updateSelectInput(session, "sel_country", 
                        selected = unlist(countries[input$sel_country]))
    }
  })

}

shinyApp(ui, server)
Aurèle
  • 11,334
  • 1
  • 29
  • 47