Skip to contents

A shinyfilters object holds a data.frame and all of the data's filters. To create one, call shinyfilters(). To modify one, use with_filters(), with_args(), with_defaults(), or with_ns().

Usage

The object behaves much like the data.frame that defines it.

filters <- shinyfilters(nyc_flights)

Names

names(filters) provides the names of the data.frame.

Shape

dim(filters), length(filters), nrow(filters), and ncol(filters) describe the dimensions.

Data

as.data.frame(filters) returns the data.

Subsetting

filters$col creates the input for one filter.

filters[[cols]] creates the inputs for all filters in cols.

filters[cols] subsets a shinyfilters object, returning only filters matching cols.

shinyfilters supports <tidy-select> in both [ and [[

In a shiny app

A shinyfilters object can be placed directly inside a ui:

library(bslib)
ui <- page_sidebar(sidebar = filters)


To enable interdependent filters, call shinyfilters_server in the server:

server <- function(...) {
  shinyfilters_server(filters)
}


Creating filters

Sometimes, you will have to create the filters as you place them in the app. The following functions create filters:

ui <- function(request) {
  page_sidebar(sidebar = filterInput(filters))
}


Currently, it is known that you must create the filters when:

  1. Bookmarking is enabled

  2. When placing a filter inside bslib::accordion.

In these instances, shinyfilters will intercept and throw an informative error.

accordion(shinyfilters(nyc_flights))
#> Error:
#> ! <shinyfilters> objects cannot be used in some shiny functions.
#> i Use `[[`, `$`, or `filterInput()` to render the filters directly.


Printing

Printing shows the input each column receives:

shinyfilters(nyc_flights)
#> <shinyfilters> * 7 filters
#>
#> Filters
#>   date       <date>  dateRangeInput
#>   carrier    <chr>   selectizeInput
#>   origin     <fct>   selectizeInput
#>   dest       <chr>   selectizeInput
#>   dep_delay  <dbl>   sliderInput
#>   distance   <dbl>   sliderInput
#>   delayed    <lgl>   selectizeInput


Any modifications will be displayed:

shinyfilters(nyc_flights, range = FALSE) |>
  with_filters(date = "date") |>
  with_ns("sidebar-mod")
#> <shinyfilters> * 7 filters * namespace "sidebar-mod"
#>
#> Filters
#>   *  date       <date>  dateInput
#>      carrier    <chr>   selectizeInput
#>      origin     <fct>   selectizeInput
#>      dest       <chr>   selectizeInput
#>      dep_delay  <dbl>   sliderInput
#>      distance   <dbl>   sliderInput
#>      delayed    <lgl>   selectizeInput
#>
#> Default Overrides
#>   range = FALSE
#>
#> * Filter set by `with_filters()`