Learn R Programming

shinyfilters (version 0.3.0)

apply_filters: 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,
  ...
)

Value

A filtered object, or a named list of filtered objects if expanded = TRUE.

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.

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

Examples

Run this code
library(S7)
df <- data.frame(
 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)

Run the code above in your browser using DataLab