Dynamic hierarchical shiny inputs

I'm trying to create an app where a user can (1) choose any number of filters to apply by selecting a column from the dataframe (2) then based on the column selected they can filter by some number/category

Here I've used map to create a sequence of filters. The first filter (column names) and second conditions will always be the same but I'm struggling to create a conditional second column based on the first and I'm open to suggestions for implementing this!

library(shiny)
library(purrr)
library(dplyr)

df <- iris


single_filter <- function(x, label, choices1, choices2) {
  fluidRow(
    column(3, selectInput(paste("filter1_", x), label = NULL, choices = choices1)),
    column(3, selectInput(paste("condition_", x), label = NULL, choices = c("==", "!="))),
    column(3, selectInput(paste("filter2_", x), label = NULL, choices = choices2, multiple = TRUE))
  )
}

ui <- fluidPage(
  single(),
  numericInput("n", "Number of filters", value = 1, min = 1),
  uiOutput("col")
)

server <- function(input, output, session) {
  
  col_names <- reactive(paste0("col", seq_len(input$n)))

  output$col <- renderUI({
    map(col_names(), ~ single_filter(.x, label = NULL, choices1 = colnames(df), choices2 = c(1,2,3)))
  })
  
}

shinyApp(ui, server)

What should choices2 be --- I think I need to use the filter1_x to subset the df column I want to be represented in the second dropdown? Any help appreciated!

You may be interested in this

2 Likes

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