Adding an "ALL" option to dynamic selectInputs

Viewed 74

I'm trying to add an ALL option in both of my dynamically linked selectInput widgets, which filters through to a graph in my main panel. Basically, I want the user to select a Service, e.g. Online or Mail-Order, or all services, and similarly with the Prof.Los column, I want the user to select either Profit or Loss, or a sum of both.

I'm not sure how to add this extra option when the two Inputs are dynamically linked. So my question is, how can I include an option to filter my data by ALL variables in both drop-down menus?

My library and data:

library("shiny")
library("plotly")
library("tidyverse")
library("dplyr")

data <- data.frame(Services = c("Online","Online","Online","Online","Online","Online","Online","Online","Online", "Online","Online","Online","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Mail-order","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop","Shop"),
               Month = c("2013-01-01","2013-02-01","2013-03-01","2013-04-01","2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01","2013-01-01","2013-02-01","2013-03-01","2013-04-01","2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01","2013-01-01","2013-02-01","2013-03-01","2013-04-01","2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01"),
               Sales = c(40,50,20,30,40,50,200,100,250,100, 120,130,40,80,20,30,30,50,400,100,150,100,75,50,100,50,700,30,40,50,100,120,220,100,75,150),
               Prof.Los = c("Profit","Loss","Profit","Profit","Loss","Loss","Loss","Profit","Profit","Loss","Loss","Profit","Profit","Loss","Profit","Loss","Profit","Loss","Loss","Loss","Profit","Loss","Loss","Profit","Profit","Profit","Loss","Loss","Loss","Profit","Profit","Loss","Loss","Loss","Profit","Loss"))

My code:

UI

ui <- fluidPage(
   # App title ----
   titlePanel(h1("Analyser tool")),
   # Sidebar layout with input and output definitions ----
   sidebarLayout(
      sidebarPanel(
                   # Input: Select service type ----
                   uiOutput("services"),
                   uiOutput("rev")
      ),
      # Main panel for displaying outputs ----
      mainPanel(
         # Output: Tabset w/ plot, summary, and table ----
         tabsetPanel(type = "tabs",
                     tabPanel("Bar Chart", plotlyOutput("Plot", height = "450px"))
         )
      )
   ))

Server

server <- function(input, output) {

   # Service analyser reactive dataset
   output$services = renderUI({
      selectInput("services",
                  "Select services:",
                  choices = unique(data$Services))
   })

  ServiceSub <- reactive({
      data %>%
         filter(Services == input$services)
   })

  output$rev = renderUI({

     selectInput(inputId = "rev",
                 label = "Profit/loss",
                 choices = unique(ServiceSub()[,"Prof.Los"]),
                 selected = unique(ServiceSub()[,"Prof.Los"]))
  })

  RevSub <- reactive({
     req(input$services)
     filter(ServiceSub(), Prof.Los %in% input$rev)

  })

  output$Plot = renderPlotly({

     # plotly code
     plot_ly(RevSub(), x = ~Month, y = ~Sales, type = "bar")

  })

}
  # Create Shiny app ----
  shinyApp(ui, server)
1 Answers

You can use pickerInput from {shinyWidgets}.

I ran your code, with the following adjustments:

pickerInput("services",
            "Select services:",
            choices = unique(data$Services),
            multiple = TRUE,
            selected = NULL,
            options = list(
                  title = "Services",
                  #"max-options" = 1,
                  `actions-box` = TRUE,
                  `deselect-all-text` = "Remove"
                ))

pickerInput(inputId = "rev",
            label = "Profit/loss",
            choices = unique(ServiceSub()[,"Prof.Los"]),
            selected = unique(ServiceSub()[,"Prof.Los"]),
            multiple = TRUE,
            options = list(
                  title = "Profit/loss",
                  #"max-options" = 1,
                  `actions-box` = TRUE,
                  `deselect-all-text` = "Remove"
                ))
   
Related