View source: R/util_expect_scalar.R
util_expect_scalar | R Documentation |
check, if a scalar/vector function argument matches expectations
util_expect_scalar(
arg_name,
allow_more_than_one = FALSE,
allow_null = FALSE,
allow_na = FALSE,
min_length = -Inf,
max_length = Inf,
check_type,
convert_if_possible,
conversion_may_replace_NA = FALSE,
dont_assign = FALSE,
error_message
)
arg_name |
the argument |
allow_more_than_one |
allow vectors |
allow_null |
allow NULL |
allow_na |
allow |
min_length |
minimum length of the argument's value |
max_length |
maximum length of the argument's value |
check_type |
a predicate function, that must return |
convert_if_possible |
if given, a lambda can be given
similar to |
conversion_may_replace_NA |
if set to |
dont_assign |
set |
error_message |
if |
the value of arg_name – but this is updated in the calling frame anyway.
Other robustness_functions:
util_as_valid_missing_codes()
,
util_check_one_unique_value()
,
util_correct_variable_use()
,
util_empty()
,
util_ensure_character()
,
util_ensure_in()
,
util_ensure_suggested()
,
util_fix_rstudio_bugs()
,
util_is_integer()
,
util_is_numeric_in()
,
util_is_valid_missing_codes()
,
util_match_arg()
,
util_observations_in_subgroups()
,
util_only_NAs()
,
util_stop_if_not()
,
util_warn_unordered()
## Not run:
f <- function(x) {
util_expect_scalar(x, check_type = is.integer)
}
f(42L)
try(f(42))
g <- function(x) {
util_expect_scalar(x, check_type = is.integer, convert_if_possible =
as.integer)
}
g(42L)
g(42)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.