tidy_filter_null: Filter data frame when filter can be 'NULL'

Description Usage Arguments Details Value Examples

View source: R/tidy_filter_null.R

Description

For use inside dplyr::filter() operations where sometimes it is desired to filter nothing.

Usage

1
2
3
4
5
tidy_filter_null(
  x,
  filter_value = NULL,
  filter_how = c("==", ">=", ">", "<=", "<", "in")
)

Arguments

x

A string or vector of strings with the column name(s) to apply the filter on.

filter_value

A value (or vector when filter_how == "in") to filter by.

filter_how

A string with the filtering operation: "==", ">=", ">", "<=", "<" or "in", where "in" is for applying vector filters with %in%.

Details

This function is useful when there is a dplyr::filter() operation on a data frame but it is not always desirable to filter the data frame. A good example is a Shiny dashboard that returns a plot or table of the filtered data or of the whole dataset, depending on the input from the user.

Value

The filtered data frame or the original data frame when filter_value is NULL or NA.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
library(experienceAnalysis)
# Filter gear and carb to be 2 or 3
mtcars %>%
  dplyr::filter(
    dplyr::across(
      c("gear", "carb"),
      ~ experienceAnalysis::tidy_filter_null(., filter_value = 2:3,
                                              filter_how = "in")
    )
)

# Filter nothing (returns original data frame)
mtcars %>%
  dplyr::filter(
    dplyr::across(
      c("gear", "carb"),
      ~ experienceAnalysis::tidy_filter_null(., filter_value = NULL)
    )
)

CDU-data-science-team/experienceAnalysis documentation built on Dec. 17, 2021, 12:53 p.m.