assert_factor_vector {assertions} | R Documentation |
Assert input is a factor
Description
Assert an R object is a factor. Note that no assert_factor function exists since in R factors are always vector quantities (never scalar / in matrices)
Usage
assert_factor_vector(
x,
msg = NULL,
call = rlang::caller_env(),
arg_name = NULL
)
Arguments
x |
An object |
msg |
A character string containing the error message to display if |
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). |
Details
Technically this function name is misleading, since is.vector(factor(1)) == FALSE
but since they act exactly like vectors to end users, I think this name is the most suitable
Value
invisible(TRUE) if x
is a factor, otherwise aborts with the error message specified by msg
Examples
try({
assert_factor_vector(factor(c("a", "b", "c"))) # Passes
assert_factor_vector(c("a", "b", "c")) # Throws default error
assert_factor_vector(factor(c("a", "b", "c")), "Custom error message") # Passes
assert_factor_vector(1:3, "Custom error message") # Throws custom error
})
[Package assertions version 0.1.0 Index]