assert_create {assertions} | R Documentation |
Create an assertion function
Description
This function creates an assertion function that can be used to check the
validity of an input.
All assertions provided with this package are created using either assert_create()
or assert_create_chain()
Usage
assert_create(func, default_error_msg = NULL)
Arguments
func |
A function defining the assertion criteria. This function should
return a logical value ( |
default_error_msg |
A character string providing an error message in case
the assertion fails. Must be supplied if function
|
Value
An assertion function.
Examples
#' # Create an assertion function that checks that a character string is all
# lower case
assert_character <- assert_create(
is.character,
"{arg_name} must be a character vector, not a {class(arg_value)}"
)
# Use the assertion function
try({
is_lower("hello") # Returns invisible TRUE
is_lower("Hello") # Aborts the function with the error message
})