When creating functions for other people to use, you always need some validator functions which check if arguments passed by users are valid, and if not, generate informative and good-formatted error messages in a consistent style.
Functions documented here serve the exact purposes:
check_type()
checks if an argument has valid type.
check_types()
checks if each item of an argument has valid type.
check_class()
checks if an argument has valid class.
check_classes()
checks if each item of an argument has valid class.
check_length()
checks if an argument has valid length.
check_content()
checks if an argument is from some given choices.
check_string()
checks if an argument is a single character.
It can be used to check names, for example.
check_n()
checks if an argument is a single positive integer.
It can be used to check indices, for example.
check_bool()
checks if an argument is TRUE
or FALSE
.
check_type( x, valid, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... ) check_types(x, valid, name = NULL, general = NULL, supplement = NULL, ...) check_class( x, valid, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... ) check_classes(x, valid, name = NULL, general = NULL, supplement = NULL, ...) check_length( x, valid, interval = NULL, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... ) check_content( x, valid, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... ) check_string( x, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... ) check_n( x, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... ) check_bool( x, name = NULL, general = NULL, specifics = NULL, supplement = NULL, ... )
x | The argument to be checked.
|
---|---|
valid |
|
name | Optional. A single character which represents the argument's
name. The name is used in the error message. By default, the name of the
argument passed to argument |
general | Optional. A single character which represents the general
statement of the error. Each validator function comes with its own
error message, so usually you don't need to specify this argument and
arguments |
specifics | Optional. A (named) character vector which represents details of the error. |
supplement | Optional. A single character which represents the additional message added at the end of the error message. |
... | Optional. Additional arguments passed to |
interval | Only used in |
An invisible NULL
if the argument is valid,
or error message is generated.
Statement()
for more details about arguments general
,
specifics
and supplement
.
rlang::abort()
for adding additional arguments.
vignette("erify", package = "erify")
for a gentle introduction to this
package.
The tidyverse style guide for more details about the used error message style.
# argument to be checked arg <- 1:10 # invisible `NULL` returned if `arg` is valid check_type(arg, "integer") if (FALSE) { # check if `arg` is character or double check_type(arg, c("character", "double")) # check if `arg` has length 1 check_length(arg, 1) # check if `arg` has length smaller than 3 check_length(arg, c(NA, 3)) # specify argument's name check_type(arg, "character", "`other_name`") # customize error message check_type( arg, "character", general = "General statement of the error.", specifics = c(x = "An error.", i = "A hint.", `*` = "Other detail."), supplement = "More words to say." ) } # add and retrieve additional arguments tryCatch( {check_type(arg, "character", your_arg = "your data")}, error = function(e) e$your_arg )#> [1] "your data"