Compare two quantities a and b with a given comparison operator, returning a single TRUE if b is NULL.
Source: R/utils.R
nullable_comparison.RdUseful for letting x {comparison} NULL mean "match all values"
in calls to dplyr::filter() and similar functions.
Value
A logical vector. Equivalent to b {comparison_operator} a
if b is not NULL and to TRUE if b is NULL.
Examples
x <- 6
nullable_comparison(5, ">", x)
#> [1] FALSE
x <- NULL
nullable_comparison(5, ">", x)
#> [1] TRUE
df <- tibble::tibble(y = 1:6)
x <- 3
df |> dplyr::filter(
nullable_comparison(y, ">", !!x),
y < 5
)
#> # A tibble: 1 × 1
#> y
#> <int>
#> 1 4
x <- NULL
df |> dplyr::filter(
nullable_comparison(y, ">", !!x),
y < 5
)
#> # A tibble: 4 × 1
#> y
#> <int>
#> 1 1
#> 2 2
#> 3 3
#> 4 4