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()
Arguments
- func
A function defining the assertion criteria. This function should return a logical value (
TRUEwhen assertion is passed orFALSEwhen it fails). Alternatively, instead of returning FALSE, you can return a string which will act as the error message. In this latter case, you don't need to supply adefault_error_msg- default_error_msg
A character string providing an error message in case the assertion fails. Must be supplied if function
funcreturnsFALSEwhen assertion fails (as opposed to a string) Can include the following special terms{arg_name}to refer to the name of the variable supplied to the assertion.{arg_value}to refer to the value of the variable supplied to the assertion{code_to_evaluate}to evaluate the code within the error message. Replacecode_to_evaluatewith your code{.strong bold_text}to perform inline formatting. Replacebold_textwith your text. See cli documentation for details
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
})
#> Error in is_lower("hello") : could not find function "is_lower"
