Search code examples
rshinyshinydashboard

RShiny having only one filter available


I have a Shiny Dashboard App that shows simple plot. I´m filtering the dataframe that feeds the plot using 2 different filters.

What I want is, is it possible for the app to have it so that:

  1. I select a value in filter 1
  2. If I now filter for a value on filter 2, then filter 1 resets to it's original value
  3. Basically I´m only allowing 1 filter at a time

Thx!


Solution

  • You want to use updateSelectInput and then use observeEvent to catch when a filter value changes.

    library(shiny)
    library(shinydashboard) 
    
    rm(list=ls())
    
    #####/UI/####
    
    header <- dashboardHeader()
    sidebar <- dashboardSidebar()
    body <- dashboardBody(
      selectInput(inputId = "filter1", label = "Filter 1", selected = "ALL",
                  choices = c("ALL", "Bananas", "Apples", "Oranges")
      ),
      selectInput(inputId = "filter2", label = "Filter 2", selected = "ALL",
                  choices = c("ALL", "Carrots", "Peas", "Celery")
      )
    )
    
    ui <- dashboardPage(header, sidebar, body)
    
    #####/SERVER/####
    server <- function(session, input, output) { 
    
      observeEvent(input$filter2,{
        if (input$filter2 != "ALL") {
          updateSelectInput(session, "filter1", selected = "ALL")
          }
      })
    
      observeEvent(input$filter1,{
        if (input$filter1 != "ALL") {
          updateSelectInput(session, "filter2", selected = "ALL")
        }
      })
    
      }
    
    shinyApp(ui, server)