in_set {assertr} | R Documentation |
This function returns a predicate function that will take a single
value and return TRUE if the value is a member of the set of objects
supplied. This doesn't actually check the membership of anything–it
only returns a function that actually does the checking when called
with a value. This is a convenience function meant to return a
predicate function to be used in an assertr
assertion.
You can use the 'inverse' flag (default FALSE) to check if the
arguments are NOT in the set.
in_set(..., allow.na = TRUE, inverse = FALSE)
... |
objects that make up the set |
allow.na |
A logical indicating whether NAs (including NaNs) should be permitted (default TRUE) |
inverse |
A logical indicating whether it should test if arguments are NOT in the set |
A function that takes one value and returns TRUE
if the value is in the set defined by the
arguments supplied by in_set
and FALSE
otherwise
predicate <- in_set(3,4)
predicate(4)
## is equivalent to
in_set(3,4)(3)
# inverting the function works thusly...
in_set(3, 4, inverse=TRUE)(c(5, 2, 3))
# TRUE TRUE FALSE
# the remainder of division by 2 is always 0 or 1
rem <- 10 %% 2
in_set(0,1)(rem)
## this is meant to be used as a predicate in an assert statement
assert(mtcars, in_set(3,4,5), gear)
## or in a pipeline, like this was meant for
library(magrittr)
mtcars %>%
assert(in_set(3,4,5), gear) %>%
assert(in_set(0,1), vs, am)