Shiny reactive input for multiple input selectors

Hope someone can help me.

I'm building an app that requires multiple input selectors that needs to filter as the selection continues. I've made an example below. I think that the code goes into a loop but can't figure out what the solution is. I've looked for similar examples but spent way too much time on this problem.

Please assist.

Code:

pacman::p_load(shiny, tidyverse)

mtcars_df <- rownames_to_column(mtcars, "model")

# Define UI for application that draws a histogram
ui <- fluidPage(
    checkboxGroupInput(
        "select_cyl",
        "Select cylinder: ",
        choices = mtcars_df$cyl %>% unique(),
        selected = mtcars_df$cyl %>% unique()),
    checkboxGroupInput(
        "select_model",
        "select model: ",
        choices = mtcars_df$model %>% unique(),
        selected = mtcars_df$model %>% unique()
    ),
    plotOutput("hist_hp")
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
    mtcars_reactive <- reactive({
        filter(mtcars_df, 
               cyl %in% input$select_cyl,
                model %in% input$select_model)
    })
    
    observe({
        x <- mtcars_reactive() %>% pull(model) %>% unique()
        
        updateCheckboxGroupInput(session = session,
                          "select_model",
                          choices = x,
                          selected = x)
    })
    
    output$hist_hp <- renderPlot({
        
        x <- mtcars_reactive()
        
        hist(x$hp)
    })
    

}

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

I think it is because you use updateSelectInput instead of updateCheckboxGroupInput

Thank you, I've updated the code to updateCheckboxGroupInput but a new problem surfaced. When I make changes to the selection on the cyl selection then the model changes does happen but only once. Second time the there is no change. It must be a reactive issue?

Can anyone assist me with this problem on reactivity? The list does not remain reactive once a selection was made.

This may not be the best approach... but it's the way I know how to do it and it works for me:

pacman::p_load(shiny, tidyverse)

mtcars_df <- rownames_to_column(mtcars, "model")

Define UI for application that draws a histogram

ui <- fluidPage(
checkboxGroupInput(
"select_cyl",
"Select cylinder: ",
choices = mtcars_df$cyl %>% unique(),
selected = mtcars_df$cyl %>% unique()),

uiOutput("model"),
plotOutput("hist_hp")
)

Define server logic required to draw a histogram

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

datasub<-reactive({
foo <- subset(mtcars_df, cyl == input$select_cyl)
return(foo)
})

output$model<-renderUI({
selectizeInput("select_model","select model:",
choices = unique(datasub()$model),
selected = unique(datasub()$model[1]))
})

datasub2<-reactive({
foo<-subset(datasub(),model == input$select_model)
return(foo)

})

output$hist_hp <- renderPlot({

x <- datasub2()

hist(x$hp)

})

}

Run the application

shinyApp(ui = ui, server = server)

Let me know if this isn't what you were looking for!