2

I'm wondering if there is a way to use pickerInput or selectInput or similar method (ideally) or a button like select all (alternatively) to select multiple items from a list with a single click. For example, I would like to select a predefined list of 20 countries out of 100+ countries using a single click. In the code below, this would essentially be like selecting "USA" and "CAN" and "SYR" with a single click.

I have come across similar questions e.g. here that suggest this sort of group select is possible. However, I'm having a hard time understanding it and was also wondering if there was a simpler way to achieve this sort of group/multi select.

Any help would be greatly appreciated.

Thankyou

library(shiny)
library(dplyr)
library(tidyverse)

df = data.frame("Country" = c("USA","USA","CAN","CAN","AFG","AFG","SYR","SYR"),
                "Year" = c(2000,2001,2000,2001,2000,2001,2000,2001),
                "Variable_1" = c(10,12,14,16,10,11,12,13),
                "Variable_2" = c(20,19,18,17,20,21,22,23),
                "Variable_3" = c(13,13,14,14,16,16,12,12))

#df_long <- melt(df, id=c("Country","Year"))  

ui = fluidPage(
  titlePanel("My Dashboard"), 
  
  pickerInput("myvariable","Pick variables", choices =c("Variable_1","Variable_2","Variable_3"),
              options =list("actions-box" = TRUE),
              multiple=TRUE, 
              selected = "Variable_1"),
  
  sliderInput("year_selector", "Select Year Range",min = 2000,max = 2003,value = c(2000, 2013)),
  
  pickerInput("choicePicker","Pick countries",choices =c("USA", "AFG","SYR","CAN"),
              options =list("actions-box" = TRUE), 
              multiple=TRUE,
              selected="SYR"),
  
  plotOutput("trend")
    )
  
server = function(input, output, session){
  selected <- reactive(filter(df, Country %in% input$choicePicker, Year>=input$year_selector[1], Year<=input$year_selector[2]))
  output$trend = renderPlot({
    ggplot(selected(), aes_string(x="Year", y=input$myvariable, color="Country", group="Country")) +
      geom_line(size = 2) +   
      scale_x_continuous(breaks = pretty_breaks()) +
      labs(x = "",
           y = paste0(input$myvariable),
           title = paste(" ,", " "),
           caption = paste(" ", " ")) +
      theme(legend.position = c(0.8, 0.8))
  })
}

shinyApp(ui=ui, server=server)
thehand0
  • 1,123
  • 4
  • 14
  • See [this question](https://stackoverflow.com/questions/67908249/is-there-a-way-to-select-an-entire-group-of-choices-on-a-pickerinput-from-shinyw/67925520#67925520) - sounds like you're after the same thing. – phalteman Jun 10 '21 at 17:09

0 Answers0