-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
  • 12,545
  • 1
  • 31
  • 49
  • 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
  • 12,545
  • 1
  • 31
  • 49
  • thank you very much, can't I just make them in the same drop down menu ? like this in this screenshot: – Jana Zuraik Jul 19 '18 at 07:48
  • how can I add a picture here please? – Jana Zuraik Jul 19 '18 at 07:48
  • and regarding this countries <- list( "Central" = list("Country A","Country B"), "Non central" = list("Country C","Country D")) Do I have to write them manually the 11 countries? even though there's a column called Office country that contains these countries.. it contains 2701 observations but the total number of countries used are 11. so do I still have to do this preparation manually (of list() function and cental/noncentral? ain't there a way of accessing these names, though they're in dataset, and I would like to assign them directly to the code, no ? what do you think – Jana Zuraik Jul 19 '18 at 10:51
  • is there any other way that we can chat? – Jana Zuraik Jul 19 '18 at 10:59
  • Yes, we can do so in the r-public chat room: https://chat.stackoverflow.com/rooms/25312/r-public – Aurèle Jul 19 '18 at 11:10
  • i couldn't use the chat room because I need to have 20 reputation, and I have only 1. because I am still new to stackoverflow – Jana Zuraik Jul 24 '18 at 08:59
  • Indeed you shouldn't have to do it manually. If you have a data frame with columns `region` and `country`, that could look like: `df <- data.frame( region = rep(c("Central", "Non central"), each = 3), country = c("Country A", "Country A", "Country B", "Country D", "Country D", "Country C"), stringsAsFactors = FALSE ) ; countries <- lapply(split(df, df$region), function(x) unique(x$country))` – Aurèle Jul 24 '18 at 15:17