| assert_character_scalar | R Documentation |
Checks if an argument is a character scalar and (optionally) whether it matches
one of the provided values.
assert_character_scalar(
arg,
values = NULL,
case_sensitive = TRUE,
optional = FALSE,
arg_name = rlang::caller_arg(arg),
message = NULL,
class = "assert_character_scalar",
call = parent.frame()
)
arg |
A function argument to be checked
|
values |
A
|
case_sensitive |
Should the argument be handled case-sensitive?
If set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
class |
Subclass of the condition. |
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
The function throws an error if arg is not a character vector or if arg
is a character vector but of length > 1 or if its value is not one of the values
specified. Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(msg_type) {
assert_character_scalar(msg_type, values = c("warning", "error"))
}
example_fun("warning")
try(example_fun("message"))
try(example_fun(TRUE))
# handling arguments case-insensitive
example_fun2 <- function(msg_type) {
msg_type <- assert_character_scalar(
msg_type,
values = c("warning", "error"),
case_sensitive = FALSE
)
if (msg_type == "warning") {
print("A warning was requested.")
}
}
example_fun2("Warning")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.