Andriy T.
Andriy T.

Reputation: 2030

R Shiny filter on data frame

I need to apply a filter on data frame in my Shiny App. I am looking for some button (small one) that opens a multiselect list of values of a specific column. Something like Excel's table filter

Excel pivot table filter

As an example (from another topic):

library(shiny)
shiny::runApp(list(
  ui = fluidPage(
    checkboxGroupInput("specy", "Specy", choices = levels(iris$Species)),
    tableOutput("content")
  ),
  server = function(input, output, session) {
    output$content <- renderTable({
      iris[iris$Species == input$specy, ]
    })
  }
))

Some idea from the widget fallery: use checkboxGroupInput that appears clicking on actionButton

All kind of suggestions are welcome. Thank's

Upvotes: 2

Views: 4127

Answers (1)

Quinn Weber
Quinn Weber

Reputation: 927

This gets you most of the way, but it doesn't have a way to hide the checkbox once you have selected an option:

library(shiny)
shiny::runApp(list(
  ui = fluidPage(
    actionButton("show_checkbox", "Show Choices"),
    uiOutput("checkbox"),
    tableOutput("content")
  ),
  server = function(input, output, session) {
    output$checkbox <- renderUI({
        if ( is.null(input$show_checkbox) ) { return(NULL) }
        if ( input$show_checkbox == 0 ) { return(NULL) }
        return(checkboxGroupInput("specy", "Specy", choices = levels(iris$Species)))
    })
    output$content <- renderTable({
        if ( is.null(input$specy) ) { return(iris) }
        if ( length(input$specy) == 0 ) { return(iris) }
      iris[iris$Species == input$specy, ]
    })
  }
))

Upvotes: 3

Related Questions