Dynamic Filters in Shiny


#1

Hi - I’m trying to create an app where a given filter’s values update based on selections in another filter(s). In this basic example, I want the user to select the cylinders and then have the mpg filter only show values for the selected cylinder. I was able to get the mpg to update based on the cyl selection, but I don’t know how to display that in my output correctly. Right now, it’s just displaying the result of a reactive function dependent on only the first filter (cyl). Basically:

-what do I have to put in my output$data variable to render a table based on selections in both filters?

-is there a better way to achieve this? i.e. with renderUI functions?

Thanks for the help!



ui <- shinyUI(fluidPage(
  
  titlePanel("test"),
  
  sidebarLayout(

    
  
    sidebarPanel(
      selectInput(inputId = "cyl",
                  label = "Select Cylinders",
                  choices = unique(cars$cyl)),
      
      selectInput(inputId = "mpg",
                  label = "Select MPG",
                  choices = "NULL")),
       
  
    mainPanel(
      dataTableOutput("data")
    )
  )
))
      
      
      
    
    
  


server <- function(session, input, output){
  op1 <- reactive({
    cars %>% filter(cyl==input$cyl)
  })
  
  observe({
    x <- op1() %>% select(mpg) %>% distinct()
    updateSelectInput(session, "mpg", "Select MPG", choices = x)
  })
  
  
  output$data <- renderDataTable({
    op1()
  })
  
  }


shinyApp(ui = ui, server = server)
 



#2

Hi @romanb333, you may want to check out this article: http://shiny.rstudio.com/articles/dynamic-ui.html