KBE11416
KBE11416

Reputation: 127

Multiple filters Shiny

I'm trying to get 2 Select all filters to work together in the example below; The logic works fine for 1 filter.

##CREATE DATA

Director <- c("dir1", "dir1","dir1", "dir2", "dir2", "dir3", "dir3", "dir3")
Manager <- c("mgr1", "mgr","mgr3", "mgr4", "mgr5", "mgr6", "mgr7", "mgr8")

  df <- data.frame(Director, Manager)

  df$Sales <- sample(100, size = nrow(df), replace = TRUE)

  director_choices = c("All", c(as.character(unique(df$Director))))
  manager_choices = c("All", c(as.character(unique(df$Manager))))


ui <-  dashboardPage(
    
    # Application title
    dashboardHeader(title = "Sales"),
    
    # Sidebar with a slider input 
    dashboardSidebar(
      sidebarMenu(
        menuItem("Maps",
                 tabName = "Maps",
                 icon = icon("dashboard")),
        hr(),
        selectInput("Director", "Director", choices = director_choices, multiple = T, selected = "All"),
        selectInput("Manager", "Manager", choices = manager_choices, multiple = T)

The filter below gets me the correct filtering just at the director level, my goal is to have a select all filter at the manager level that reacts to the director filer.

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

    observe({
      if("All" %in% input$Director)
        selected_choices = Director[-1]
      else
        selected_choices = input$Director
      updateSelectInput(session, "Director", selected = selected_choices)
    })

  # Run the application 
  shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 1262

Answers (1)

YBS
YBS

Reputation: 21349

You don't need to update the first selectInput. Just the second one should suffice. Try this.

##CREATE DATA

Director <- c("dir1", "dir1","dir1", "dir2", "dir2", "dir3", "dir3", "dir3")
Manager <- c("mgr1", "mgr2","mgr3", "mgr4", "mgr5", "mgr6", "mgr7", "mgr8")

df <- data.frame(Director, Manager)

df$Sales <- sample(100, size = nrow(df), replace = TRUE)

director_choices = c("","All", c(as.character(unique(df$Director))))
manager_choices = c(as.character(unique(df$Manager)))

ui <-  dashboardPage(
  
  # Application title
  dashboardHeader(title = "Sales"),
  
  # Sidebar with a slider input 
  dashboardSidebar(
    sidebarMenu(
      menuItem("Maps",
               tabName = "Maps",
               icon = icon("dashboard")),
      hr(),
      selectInput("Director", "Director", choices = director_choices, multiple = T, selected = "All"),
      selectInput("Manager", "Manager", choices = manager_choices, multiple = T)
    )
  ),
  dashboardBody()
)

server <- function(input, output, session) {
  
  observe({
    df1 <- df[df$Director %in% input$Director,]
    if (is.null(input$Director)) {selected_choices = ""
    }else if("All" %in% input$Director) {selected_choices = manager_choices
    }else selected_choices = unique(df1$Manager)
    
    updateSelectInput(session, "Manager", choices = selected_choices)
  })
  
}
  
# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions