Package 'shinyfilters'

Title: Create 'shiny' Inputs from Vectors, 'data.frames', or any R Object
Description: Provides an interface to 'shiny' inputs used for filtering vectors, data.frames, and other objects. 'S7'-based implementation allows for seamless extensibility.
Authors: Josh Livingston [cre, aut], Novica Nakov [ctb] (ORCID: <https://orcid.org/0009-0005-7773-7718>)
Maintainer: Josh Livingston <[email protected]>
License: MIT + file LICENSE
Version: 0.3.1.9000
Built: 2026-10-18 18:26:06 UTC
Source: https://github.com/joshwlivingston/shinyfilters

Help Index


Apply Filters to an object

Description

Applies a list of filters to an object, returning the filtered object.

Usage

apply_filters(
  x,
  filter_list,
  filter_combine_method = "and",
  expanded = FALSE,
  cols = NULL,
  ...
)

Arguments

x

An object to filter; typically a data.frame.

filter_list

A named list of filter values, used to filter the values in x. If filter_list is NULL, x is returned unmodified. A missing value in x matches a filter only when NA is one of its values.

filter_combine_method

A string or function indicating how to combine multiple filters. If a string, it can be "and" (or "&") for logical AND, or "or" (or "|") for logical OR. If a function, it should take two logical vectors and return a combined logical vector.

expanded

Logical; if TRUE, returns a named list of data.frames, each containing one column, its own, filtered according to the values of all other filters.

cols

Optional character vector of column names to retain in the output when x is a data.frame. If NULL (the default), all columns are retained.

...

Additional arguments passed to get_filter_logical().

Value

A filtered object, or a named list of filtered objects if expanded = TRUE. In a filtered data frame, a column whose class has no [ method keeps its attributes, such as a class that chooses its input.

Examples

library(S7)
df <- data.frame(
 stringsAsFactors = FALSE,
 category = rep(letters[1:3], each = 4),
 value = 1:12,
 date = as.Date('2024-01-01') + 0:11
)
filters <- list(
  category = c("a", "b"),
  value = c(3, 11)
)

# Apply filters with logical AND
apply_filters(df, filters, filter_combine_method = "and")

# Apply filters with logical OR
apply_filters(df, filters, filter_combine_method = "or")

# Get expanded filters
apply_filters(df, filters, expanded = TRUE)

Derive Arguments for shiny Inputs

Description

Provides the appropriate function arguments for the input function selected by filterInput() or updateFilterInput().

Usage

args_filter_input(x, ...)

args_update_filter_input(x, ...)

Arguments

x

The object being passed to filterInput() or updateFilterInput().

...

Additional arguments passed to the method. See details.

Details

The following arguments are supported in ...:

range

(Date, POSIXt). Logical. If TRUE, args_filter_input() will provide the arguments for range date inputs. Only applies when x is of class Date or POSIXt.

textbox

(character). Logical. If FALSE (the default), args_filter_input() will provide the arguments for select inputs.

choices_asis

(character, factor, logical). Logical. If TRUE, the choices provided to select inputs will not be modified. If FALSE (the default), duplicate values will be removed and the choices will be sorted. Only applies when x is of class character, factor, or logical.

server

If TRUE, indicates that the choices will be provided server-side. In this case, arguments are not computed for args_filter_input(). Ignored in args_update_filter_input().

args_unique

An optional named list of arguments passed to unique(), called when x is a character, factor, or logical, textbox = FALSE, and choices_asis = FALSE.

args_sort

An optional named list of arguments passed to sort(), which is called after unique().

Value

A named list of arguments for a shiny input function

Examples

args_filter_input(iris$Petal.Length)

Prepare and Evaluate Input Function and Arguments

Description

Internal function used to prepare input arguments using args_filter_input(), and gracefully pass them to provided input function.

Usage

call_filter_input(x, .f, ...)

call_update_filter_input(x, .f, ...)

Arguments

x

The object being passed to filterInput().

.f

The input function to be called.

...

Arguments passed to either args_filter_input() or provided input function.

Details

call_filter_input() and call_update_filter_input() are used when customizing shinyfilters. For more, see vignette("customizing-shinyfilters").

Value

The result of calling the provided input function.

Examples

library(S7)
library(shiny)
# call_filter_input() is used inside filterInput() methods
method(filterInput, class_numeric) <- function(x, ...) {
  call_filter_input(x, sliderInput, ...)
}

# call_update_filter_input() is used inside updateFilterInput() methods
method(updateFilterInput, class_numeric) <- function(x, ...) {
  call_update_filter_input(x, updateSliderInput, ...)
}

Create a shiny Input

Description

Selects and creates a shiny input based the type of object x and other arguments.

Usage

filterInput(x, ...)

Arguments

x

The object used to create the input.

...

Arguments used for input selection or passed to the selected input. See details.

Details

The following arguments passed to ... are supported:

area

(character). Logical. Controls whether to use textAreaInput (TRUE) or textInput (FALSE, default). Only applies when textbox is TRUE.

radio

(character, factor, list, logical). Logical. Controls whether to use radioButtons (TRUE) or a dropdown input (FALSE, default). For character vectors, radio only applies if textbox is FALSE, the default.

range

(Date, POSIXt). Logical. Controls whether to use dateRangeInput (TRUE) or dateInput (FALSE, default).

selectize

(character, factor, list, logical). Logical. Controls whether to use selectizeInput (TRUE) or selectInput (FALSE, default). For character vectors, selectize only applies if textbox is FALSE, the default.

slider

(numeric). Logical. Controls whether to use sliderInput (TRUE) or numericInput (FALSE, default).

textbox

(character). Logical. Controls whether to use a text input (TRUE) or a dropdown input (FALSE, default).

ns

An optional namespace created by NS(). Useful when using filterInput() on a data.frame inside a shiny module.

Remaining arguments passed to ... are passed to the args_filter_input() or the selected input function.

Value

One of the following shiny inputs is returned, based on the type of object passed to x, and other specified arguments. See vignette("filter-input-catalog") for the full list of examples.

Value x Arguments
dateInput Date, POSIXt default
dateRangeInput Date, POSIXt range = TRUE
numericInput numeric default
radioButtons character, factor, list, logical radio = TRUE
selectInput character, factor, list, logical default
selectizeInput character, factor, list, logical selectize = TRUE
sliderInput numeric slider = TRUE
textAreaInput character textbox = TRUE, area = TRUE
textInput character textbox = TRUE

Examples

library(shiny)

ui <- fluidPage(
	 sidebarLayout(
		 sidebarPanel(
			 # Create a filterInput() inside a shiny app:
			 filterInput(
				x = letters,
				inputId = "letter",
				label = "Pick a letter:"
			 )
		 ),
		 mainPanel(
			 textOutput("selected_letter")
		 )
	 )
)

server <- function(input, output, session) {
	 output$selected_letter <- renderText({
		 paste("You selected:", input$letter)
	 })
}

shinyApp(ui, server)

Compute a Filter Predicate

Description

Computes a logical vector indicating which elements of x match the filter criteria specified by val.

Usage

get_filter_logical(x, val, ...)

Arguments

x

An object to filter; typically a data.frame.

val

The filter criteria.

...

Arguments passed to methods. See details.

Details

The following arguments are supported in ...:

column

When x is a data.frame, column is the name of the column intended to be filtered.

comparison

When x is a numeric or Date and val is a length-one numeric or Date, comparison is the function used to compare x with val. The default is <=.

gte

When x is a numeric or Date and val is a length-two numeric or Date, gte controls whether to use >= (TRUE, default) or > (FALSE) on val[[1]].

lte

When x is a numeric or Date and val is a length-two numeric or Date, lte controls whether to use <= (TRUE, default) or < (FALSE) on val[[2]].

When val is a character vector, such as the value of a select or radio input, and x isn't a character, factor, or logical vector, the elements of x are compared as text. A datetime x is compared as its date, whether val is text, a Date, or a datetime.

Value

A logical vector indicating which elements of x match the filter criteria specified by val.

Examples

df <- data.frame(
  stringsAsFactors = FALSE,
  category = rep(letters[1:3], each = 4),
  value = 1:12,
  date = Sys.Date() + 0:11
)

# Filter character column
get_filter_logical(df, c("a", "b"), column = "category")

# Filter numeric column with single value
get_filter_logical(df, 5, column = "value", comparison = `<=`)

# Filter numeric column with range
get_filter_logical(df, c(3, 8), column = "value", gte = TRUE, lte = FALSE)

Retrieve the Ids of Input Objects

Description

Returns the (unnamespaced) ids of the inputs for the provided object.

Usage

get_input_ids(x, ...)

Arguments

x

An object for which to retrieve input ids; typically a data.frame.

...

Passed onto methods.

Value

A character vector of input ids.

Examples

df <- data.frame(
  stringsAsFactors = FALSE,
  name = c("Alice", "Bob"),
  age = c(25, 30),
  completed = c(TRUE, FALSE)
)

get_input_ids(df)

Retrieve the Labels of Input Objects

Description

Returns the labels of the shiny inputs for the provided object.

Usage

get_input_labels(x, ...)

Arguments

x

An object for which to retrieve input labels; typically a data.frame.

...

Passed onto methods.

Value

A character vector of input labels

Examples

df <- data.frame(
  stringsAsFactors = FALSE,
  name = c("Alice", "Bob"),
  age = c(25, 30),
  completed = c(TRUE, FALSE)
)

get_input_labels(df)

Get Multiple Values from a shiny Input Object

Description

Retrieves multiple input values from a shiny input object based on the names provided in x.

Usage

get_input_values(input, x, ...)

Arguments

input

A shiny input object, i.e., the input argument to the shiny server.

x

A character vector of input names, or a data.frame whose column names are converted to input names via get_input_ids().

...

Passed onto methods.

Value

A named list of input values corresponding to the names in x.

Examples

library(shiny)
df <- data.frame(
	 stringsAsFactors = FALSE,
  name = c("Alice", "Bob"),
  age = c(25, 30),
  completed = c(TRUE, FALSE)
)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      filterInput(df)
    ),
    mainPanel(
      verbatimTextOutput("output_all"),
      verbatimTextOutput("output_subset")
    )
  )
)
server <- function(input, output, session) {
  output$output_all <- renderPrint({
    get_input_values(input, df)
  })
  output$output_subset <- renderPrint({
    get_input_values(input, c("name", "completed"))
  })
}
shinyApp(ui, server)

Flights departing New York City

Description

A sample of 200 flights that departed New York City airports in 2013, with a mix of column types for trying out filterInput().

Usage

nyc_flights

Format

A data frame with 200 rows and 7 columns:

date

Date of departure.

carrier

Two-letter airline code.

origin

Origin airport: "EWR", "JFK", or "LGA".

dest

Destination airport code.

dep_delay

Departure delay in minutes. Negative values are early departures; NA for cancelled flights.

distance

Distance between airports in miles.

delayed

Whether the flight arrived more than 15 minutes late; NA when the arrival time is unknown.

Source

A random sample of flights from the nycflights13 package (https://github.com/tidyverse/nycflights13), released under CC0.


Run the backend server for filterInput

Description

shinyfilters_server() runs an observer that makes your filters interdependent. serverFilterInput() is an alias of shinyfilters_server().

Usage

serverFilterInput(
  x,
  session = getDefaultReactiveDomain(),
  filter_combine_method = "and",
  args_apply_filters = NULL,
  ...,
  input = deprecated()
)

shinyfilters_server(
  x,
  session = getDefaultReactiveDomain(),
  filter_combine_method = "and",
  args_apply_filters = NULL,
  ...,
  input = deprecated()
)

Arguments

x

An object being filtered; typically the result of shinyfilters().

session

The shiny session whose inputs are read and updated. Defaults to the current session. The inputs of a configuration with a namespace (see with_ns()) are found from any session of the app, so shinyfilters_server() can be called at the top level of the server or inside the module. The namespace is the full one, as in the UI: inside a module's server that is session$ns(NULL), which differs from the module's id when modules are nested.

filter_combine_method

A string or function indicating how to combine multiple filters. If a string, it can be "and" (or "&") for logical AND, or "or" (or "|") for logical OR. If a function, it should take two logical vectors and return a combined logical vector.

args_apply_filters

A named list of additional arguments passed to apply_filters().

...

Additional arguments passed to updateFilterInput().

input

Deprecated: omit it, or use session. A shiny input object, or a reactive that resolves to a list of named values.

Details

Only the inputs that have no value are updated, and they are left without one. The inputs of a configuration made by shinyfilters() are updated as updateFilterInput() updates them, from the filtered data.

An input set by a function other than the shiny inputs filterInput() creates needs the function that updates it. A shinyWidgets input, such as shinyWidgets::pickerInput, comes with its own. A column's update sends its choices, or its minimum and maximum, and that function is given the ones it has an argument for. One with an argument for none of them, such as shinyWidgets::updateNumericRangeInput, leaves its input as it is, so the input doesn't follow the other filters. Any other input, such as shiny::checkboxGroupInput, needs its update function named with .update_fn = fn among the input's arguments, as described in with_filters(). Without it, shinyfilters_server() errors.

Value

A reactiveValues list with two elements: filtered, the filtered data, and input_values, the current filter input values as a named list.

Examples

library(bslib)
library(DT)
library(S7)
library(shiny)

must_use_radio <- new_S3_class(
	 class = "must_use_radio",
	 constructor = function(.data) .data
)
method(filterInput, must_use_radio) <- function(x, ...) {
	 call_filter_input(x, radioButtons, ...)
}
method(updateFilterInput, must_use_radio) <- function(x, ...) {
	 call_update_filter_input(x, updateRadioButtons, ...)
}

use_radio <- function(x) {
	 structure(x, class = unique(c("must_use_radio", class(x))))
}

df_shared <- data.frame(
  stringsAsFactors = FALSE,
	 x = letters,
	 y = use_radio(sample(c("red", "green", "blue"), 26, replace = TRUE)),
	 z = round(runif(26, 0, 3.5), 2),
	 q = sample(Sys.Date() - 0:7, 26, replace = TRUE)
)

filters_ui <- function(id) {
	 ns <- NS(id)
	 filterInput(
		 x = df_shared,
		 range = TRUE,
		 selectize = TRUE,
		 slider = TRUE,
		 multiple = TRUE,
		 ns = ns
	 )
}

filters_server <- function(id) {
	 moduleServer(id, function(input, output, session) {
 		# serverFilterInput() returns a reactiveValues list
 		serverFilterInput(df_shared, range = TRUE)
 	})
}

ui <- page_sidebar(
 	sidebar = sidebar(filters_ui("demo")),
 	DTOutput("df_full"),
 	verbatimTextOutput("input_values"),
 	DTOutput("df_filt")
)

server <- function(input, output, session) {
	 res <- filters_server("demo")
	 output$df_full <- renderDT(datatable(df_shared))
	 output$input_values <- renderPrint(res$input_values)
	 output$df_filt <- renderDT(datatable(apply_filters(
 		df_shared,
 		res$input_values
 	)))
}

shinyApp(ui, server)

Configure the Filters for a Data Frame

Description

shinyfilters() creates a set of interdependent filters for use in a shiny application.

Usage

shinyfilters(
  .data,
  ...,
  area = FALSE,
  radio = FALSE,
  range = TRUE,
  selectize = TRUE,
  multiple = TRUE,
  slider = TRUE,
  textbox = FALSE,
  ns = NULL
)

Arguments

.data

A data.frame.

...

Named arguments passed to the selected input.

area

(character). Logical. Controls whether to use textAreaInput (TRUE) or textInput (FALSE). Only applies when textbox is TRUE.

radio

(character, factor, list, logical). Logical. Controls whether to use radioButtons (TRUE) or a dropdown input (FALSE). For character vectors, radio only applies if textbox is FALSE. TRUE turns off the selectize default; setting both to TRUE is an error.

range

(Date, POSIXt). Logical. Controls whether to use dateRangeInput (TRUE) or dateInput (FALSE).

selectize

(character, factor, list, logical). Logical. Controls whether to use selectizeInput (TRUE) or selectInput (FALSE). For character vectors, selectize only applies if textbox is FALSE.

multiple

Passed to selectInput or selectizeInput.

slider

(numeric). Logical. Controls whether to use sliderInput (TRUE) or numericInput (FALSE).

textbox

(character). Logical. Controls whether to use a text input (TRUE) or a dropdown input (FALSE).

ns

An optional namespace created by NS() or an object to be coerced to a namespace. Useful when using shinyfilters() inside a shiny module.

Value

A shinyfilters object.

See Also

with_filters(), with_ns()

Examples

shinyfilters(nyc_flights)

# Use sliders for every numeric column
shinyfilters(nyc_flights, slider = TRUE)

'shinyfilters' Objects

Description

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()`

Update a shiny Input

Description

Updates a shiny input based the type of object x and other arguments.

Usage

updateFilterInput(x, ...)

Arguments

x

The object used to create the input, or a configuration made by shinyfilters().

...

Arguments used for input selection or passed to the selected input update function. See details.

Details

The following arguments passed to ... are supported:

area

(character). Logical. Controls whether to use updateTextAreaInput (TRUE) or updateTextInput (FALSE, default). Only applies when textbox is TRUE.

radio

(character, factor, list, logical). Logical. Controls whether to use updateRadioButtons (TRUE) or a dropdown input update function (FALSE, default). For character vectors, radio only applies if textbox is FALSE, the default.

range

(Date, POSIXt). Logical. Controls whether to use updateDateRangeInput (TRUE) or updateDateInput (FALSE, default).

selectize

(character, factor, list, logical). Logical. Controls whether to use updateSelectizeInput (TRUE) or updateSelectInput (FALSE, default). For character vectors, selectize only applies if textbox is FALSE, the default.

slider

(numeric). Logical. Controls whether to use updateSliderInput (TRUE) or updateNumericInput (FALSE, default).

textbox

(character). Logical. Controls whether to update a text input (TRUE) or a dropdown input (FALSE, default).

Remaining arguments passed to ... are passed to args_update_filter_input() or the selected input update function.

When x is a configuration made by shinyfilters(), each column's input is updated with the function and arguments that match the input filterInput() creates for it: the configuration's defaults, and the input and arguments with_filters() or with_args() set for it, computed from the configuration's data. The arguments that set an input's value (value, selected, start, and end) are left out, and the configuration's namespace is applied to the ids.

Value

The result of the following shiny input updates is returned, based on the type of object passed to x, and other specified arguments.

Value x Arguments
updateDateInput Date, POSIXt default
updateDateRangeInput Date, POSIXt range = TRUE
updateNumericInput numeric default
updateRadioButtons character, factor, list, logical radio = TRUE
updateSelectInput character, factor, list, logical default
updateSelectizeInput character, factor, list, logical selectize = TRUE
updateSliderInput numeric slider = TRUE
updateTextAreaInput character textbox = TRUE, area = TRUE
updateTextInput character textbox = TRUE

Examples

library(shiny)

fruits <- list(
	"a" = c("apples", "avocados"),
	"b" = c("bananas", "blueberries"),
	"c" = c("cherries", "cantaloupe")
)

ui <- fluidPage(
	sidebarLayout(
		sidebarPanel(
			filterInput(
				x = letters[1:3],
				inputId = "letter",
				label = "Pick a letter:",
       multiple = TRUE
			),
			filterInput(
				x = fruits,
				inputId = "fruits",
				label = "Pick a fruit:"
			)
		),
   mainPanel()
	)
)

server <- function(input, output, session) {
	observe({
		fruits_filtered <- fruits
		if (!is.null(input$letter) && length(input$letter) != 0L) {
			fruits_filtered <- fruits[input$letter]
		}
		# Call updateFilterInput() inside the shiny server:
		updateFilterInput(x = fruits_filtered, inputId = "fruits")
	})
}
shinyApp(ui, server)

Set the Arguments of Inputs

Description

with_args() sets arguments for the inputs filterInput() creates for one or more columns of a configuration made by shinyfilters(), such as a slider's value, step, or width. Each column keeps its input.

Usage

with_args(.filters, ...)

Arguments

.filters

A configuration created by shinyfilters().

...

Any number of formulas.

  • cols ~ list(arg = value, ...): the columns on the left, their arguments on the right.

Arguments replace the ones shinyfilters passes for the column, such as label, choices, min, max, and value. inputId can't be set: an input's id is always its column's name. .update_fn = fn names the function that updates the input instead, as described in with_filters().

A value can use .x, the column the input is created for. It is evaluated when the input is created, and a call in it that takes na.rm, such as range(.x), gets na.rm = TRUE unless it sets na.rm itself. Other values are evaluated right away, and can use the columns too, as they are at that point.

Details

Arguments stay with their column. Setting one again replaces it, and an input chosen later with with_filters() keeps the ones it has an argument for.

Value

The updated configuration.

See Also

with_filters() to choose inputs, with_defaults() to set arguments for every column.

Examples

filters <- shinyfilters(nyc_flights)

# A range slider: `.x` is the column the input is created for
filters <- with_args(filters, dep_delay ~ list(value = range(.x)))
filters$dep_delay

# Several arguments
with_args(filters, dep_delay ~ list(value = range(.x), step = 5))

# The same arguments for several columns
with_args(filters, c(dep_delay, distance) ~ list(value = range(.x)))

# Different arguments for different columns
with_args(
  filters,
  dep_delay ~ list(step = 5),
  origin ~ list(label = "Airport")
)

Change the Default Arguments of a Configuration

Description

with_defaults() adds, replaces, or removes the arguments that a configuration made by shinyfilters() passes to filterInput() for every column.

Usage

with_defaults(.filters, ...)

Arguments

.filters

A configuration created by shinyfilters().

...

Named arguments passed to filterInput() for every column, such as textbox = TRUE or width = "200px". An argument set to NULL, or an input flag such as slider set to FALSE, filterInput()'s default, is removed. selectize = FALSE is kept: shiny::selectInput() takes it too. Arguments not named here keep their current values.

Value

The updated configuration.

See Also

shinyfilters(), with_filters(), with_ns()

Examples

filters <- shinyfilters(nyc_flights, width = "200px")

# Turn a default off
filters <- with_defaults(filters, slider = FALSE)
filters

# Replace one
with_defaults(filters, width = "100%")

# Remove one
with_defaults(filters, width = NULL)

Choose the Input for Columns

Description

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.

See Also

shinyfilters(), with_args()

Examples

filters <- shinyfilters(nyc_flights)
filters <- with_filters(filters, origin = "radio", carrier = "selectize")
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)))

# Add a column computed from the others
with_filters(filters, delay_sq = dep_delay^2)

# Give one column the input it has by default
filters <- shinyfilters(nyc_flights, slider = TRUE)
with_filters(filters, distance = "numeric")

Change the Namespace of a Configuration

Description

with_ns() adds, replaces, or removes the namespace that a configuration made by shinyfilters() applies to its input ids. Use it to build a configuration once and reuse it in several modules.

Usage

with_ns(.filters, ns)

Arguments

.filters

A configuration created by shinyfilters().

ns

The namespace: a string, used as the id passed to shiny::NS(); a namespace created by shiny::NS(); or NULL to remove the namespace. shinyfilters_server() finds the inputs by it, so it is the full namespace of their ids.

Value

The updated configuration.

See Also

shinyfilters()

Examples

filters <- shinyfilters(nyc_flights)

# Add a namespace
filters <- with_ns(filters, "flights")
filters

# Replace it, here with a namespace created by NS()
with_ns(filters, shiny::NS("departures"))

# Remove it
with_ns(filters, NULL)