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)