assert_one_of: Check if a scalar value is one of the acceptable values

assert_one_ofR Documentation

Check if a scalar value is one of the acceptable values

Description

Assert x is one of the values of y.

Usage

assert_one_of(x, y, msg = NULL, call = rlang::caller_env(), arg_name = NULL)

Arguments

x

A scalar value to check

y

A vector of acceptable values that x can take

msg

The error message thrown if the assertion fails (string)

call

Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details.

arg_name

Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).

Value

Returns invisible(TRUE) if x is a scalar and is one of the values in y, otherwise throws an error

Examples

assert_one_of(3, 1:5) # Passes because 3 is in 1:5
assert_one_of("A", c("A", "B", "C")) # Passes because "A" is in the vector

try({
  assert_one_of("D", c("A", "B", "C")) # Throws error because "D" is not in the vector
})

assertions documentation built on April 3, 2025, 5:52 p.m.