| stop_input_type {rlang} | R Documentation |
Throw a type mismatch error
Description
stop_input_type() throws an error when an argument has the wrong
type, producing a friendly error message that includes the expected
type and the actual type of the input.
Usage
stop_input_type(
x,
what,
...,
allow_na = FALSE,
allow_null = FALSE,
show_value = TRUE,
arg = caller_arg(x),
call = caller_env()
)
Arguments
x |
The object that does not conform to |
what |
The friendly expected type as a string. Can be a character vector of expected types, in which case the error message mentions all of them in an "or" enumeration. |
... |
Additional arguments passed to |
allow_na |
If |
allow_null |
If |
show_value |
Whether to show the actual value in the error message. |
arg |
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem. |
call |
The execution environment of a currently
running function, e.g. |
Value
Throws an error, does not return.
See Also
Other input checkers:
check_data_frame(),
check_type_number,
check_type_scalar
Examples
check_character <- function(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
) {
if (!missing(x)) {
if (is_character(x)) {
return(invisible(NULL))
}
if (allow_null && is_null(x)) {
return(invisible(NULL))
}
}
stop_input_type(
x,
"a character vector",
...,
allow_null = allow_null,
arg = arg,
call = call
)
}
# Succeeds
check_character(letters)
# Fails
try(check_character(42))