SelectizeInput filters selectizeInput

Hoping someone can help - I've spent ages searching for the answer online but can't quite find what I need!

I'm trying to create a selectizeInput that is filtered by another selectizeInput.

I have a list of schools that are in different areas, I'd like my first selectizeInput to mean that my second only brings up a list of schools in the selected area. I've copied the inputs below.

Any ideas?

selectizeInput("LASelect", 
               label = "Filter by Area:", 
               selected = "", 
               choices = c(ASData %>% 
                             filter(!is.na(LA)) %>% 
                             select(LA) %>%
                             unique() %>%
                             arrange(LA)),
               multiple = FALSE),

       selectizeInput("SchoolSelect", 
                           label = "Filter by School:", 
                           selected = "A Primary School", 
                           choices = c(ASData %>%
                                         filter(!is.na(School)) %>% 
                                         select(School) %>%
                                         unique() %>%
                                         arrange(School)),
                           multiple = TRUE)),

You can update the selectize control based on the selection of the other control

library(shiny)
library(dplyr)
# Sample data
ASData <- data.frame(stringsAsFactors = FALSE,
                     LA = c(rep("A", 5), rep("B", 5)),
                     School = sample(letters, 10))
# Define UI for application that draws a histogram
ui <- fluidPage(

    titlePanel("Reprex"),

    sidebarLayout(
        sidebarPanel(
            selectizeInput("LASelect", 
                           label = "Filter by Area:", 
                           selected = "", 
                           choices = c(ASData %>% 
                                           filter(!is.na(LA)) %>% 
                                           select(LA) %>%
                                           unique() %>%
                                           arrange(LA) %>% 
                                           .$LA),
                           multiple = FALSE),
            
            selectizeInput("SchoolSelect", 
                           label = "Filter by School:", 
                           selected = "A Primary School", 
                           choices = c(ASData %>%
                                           filter(!is.na(School)) %>% 
                                           select(School) %>%
                                           unique() %>%
                                           arrange(School)),
                           multiple = TRUE)),

        mainPanel(
        )
    )
)

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

    observeEvent(input$LASelect, {
        updateSelectizeInput(session,
                             inputId = "SchoolSelect",
                             choices = c(ASData %>%
                                             filter(!is.na(School), LA == input$LASelect) %>%
                                             select(School) %>%
                                             unique() %>%
                                             arrange(School)))
    })
}

shinyApp(ui = ui, server = server)
2 Likes

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.