assertr (version 2.7)

in_set: Returns TRUE if value in set

Description

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.

Usage

in_set(..., allow.na = TRUE, inverse = FALSE)

Arguments

...

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

Value

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

See Also

%in%

Examples

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataCamp Workspace