Skip to contents

with_filters() sets the input that filterInput() creates for one or more columns of a configuration made by shinyfilters(), and adds or replaces columns computed from the others. When a column's input is chosen more than once, the last one wins; the arguments set for the column stay with it.

Usage

with_filters(.filters, ...)

Arguments

.filters

A configuration created by shinyfilters().

...

Either two unnamed arguments, or any number of named arguments, or formulas:

  • with_filters(.filters, col = input, ...): each name is a column.

  • with_filters(.filters, col = expression, ...): adds or replaces a column, computed from the other columns. A replaced column keeps its input. A function or a single string is always read as an input, and input ~ list(...) as an input with its arguments; any other value is the column's data. A column takes precedence over a variable of the same name.

  • with_filters(.filters, cols ~ input, ...): a two-sided formula selects columns on its left, like cols above, and names one input for all of them on its right. It can be mixed with named columns.

  • with_filters(.filters, cols ~ list(arg = value), ...): sets arguments of the inputs the columns have, as with_args() does.

Each input is either a keyword or the shiny input function it stands for, such as shiny::radioButtons() for "radio". The keywords are "area", "date", "numeric", "radio", "range", "select", "selectize", "slider", and "textbox". "date", "numeric", and "select" are the inputs columns have by default: use them to opt a column out of a default argument such as slider = TRUE. "radio", "select", and "selectize" work with any column: a number or a date uses its sorted unique values as choices, and a datetime its dates.

Other functions are called like call_filter_input(): they receive the arguments args_filter_input() returns for the column's type, plus any other arguments they accept.

An input takes its arguments with it, wherever an input goes. Write input ~ list(...), such as col = "slider" ~ list(value = range(.x)) or cols ~ "slider" ~ list(value = range(.x), step = 5). It is read as written and never run; with_args() describes the arguments.

shinyfilters_server() needs the function that updates an input that isn't one filterInput() creates. A shinyWidgets input comes with its own, such as updatePickerInput() for pickerInput(). For any other, name it with .update_fn = fn among the arguments, such as col = checkboxGroupInput ~ list(.update_fn = updateCheckboxGroupInput).

Arguments stay with the column: setting it again adds to them, replacing those of the same name, and an input chosen later keeps the ones it has an argument for. They are matched by name only, so set an argument again if its value doesn't suit the new input. .update_fn stays with the input instead: a new input drops it.

Value

The updated configuration.

Examples

filters <- shinyfilters(nyc_flights)
filters <- with_filters(filters, origin = "radio", carrier = "selectize")
filters
#> <shinyfilters> • 7 filters
#> 
#> Filters
#>      date       <date>  dateRangeInput
#>   ●  carrier    <chr>   selectizeInput
#>   ●  origin     <fct>   radioButtons  
#>      dest       <chr>   selectizeInput
#>      dep_delay  <dbl>   sliderInput   
#>      distance   <dbl>   sliderInput   
#>      delayed    <lgl>   selectizeInput
#> 
#> ● Filter set by `with_filters()`

# Choose one input for several columns
filters <- with_filters(filters, dep_delay:origin ~ "slider")

# Give an input its arguments. `.x` is the column the input is for.
with_filters(filters, dep_delay ~ "slider" ~ list(value = range(.x)))
#> <shinyfilters> • 7 filters
#> 
#> Filters
#>      date       <date>  dateRangeInput
#>   ●  carrier    <chr>   selectizeInput
#>   ●  origin     <fct>   ✖ "slider" isn't available for <factor> columns.
#>   ●  dest       <chr>   ✖ "slider" isn't available for <character> columns.
#>   ●  dep_delay  <dbl>   sliderInput   
#>                           value = range(.x, na.rm = TRUE)
#>      distance   <dbl>   sliderInput   
#>      delayed    <lgl>   selectizeInput
#> 
#> ● Filter set by `with_filters()`

# Add a column computed from the others
with_filters(filters, delay_sq = dep_delay^2)
#> <shinyfilters> • 8 filters
#> 
#> Filters
#>      date       <date>  dateRangeInput
#>   ●  carrier    <chr>   selectizeInput
#>   ●  origin     <fct>   ✖ "slider" isn't available for <factor> columns.
#>   ●  dest       <chr>   ✖ "slider" isn't available for <character> columns.
#>   ●  dep_delay  <dbl>   sliderInput   
#>      distance   <dbl>   sliderInput   
#>      delayed    <lgl>   selectizeInput
#>   +  delay_sq   <dbl>   sliderInput   
#> 
#> ● Filter set by `with_filters()`
#> + Filter added by `with_filters()`

# Give one column the input it has by default
filters <- shinyfilters(nyc_flights, slider = TRUE)
with_filters(filters, distance = "numeric")
#> <shinyfilters> • 7 filters
#> 
#> Filters
#>      date       <date>  dateRangeInput
#>      carrier    <chr>   selectizeInput
#>      origin     <fct>   selectizeInput
#>      dest       <chr>   selectizeInput
#>      dep_delay  <dbl>   sliderInput   
#>   ●  distance   <dbl>   numericInput  
#>      delayed    <lgl>   selectizeInput
#> 
#> ● Filter set by `with_filters()`