Validation - Character vectors
test_character.RdTest if an object is a character vector.
test_character() is the predicate test, while assert_character() validates
its input, aborting if it fails the test.
Usage
test_character(
x,
len = NULL,
n_na = NULL,
n_dup = NULL,
n_char = NULL,
set = NULL,
match = NULL,
sorted = NULL,
sentinels = NULL,
custom = NULL,
perl = FALSE
)
assert_character(
x,
len = NULL,
n_na = NULL,
n_dup = NULL,
n_char = NULL,
set = NULL,
match = NULL,
sorted = NULL,
sentinels = NULL,
custom = NULL,
perl = FALSE,
action = "abort",
env = caller_env(),
x_name = NULL,
short_circuit = TRUE,
report_untested = TRUE,
args_cnd = list()
)Arguments
- x
[
any] An object to test.- len, n_na, n_dup, n_char
[
numeric()|\(){}|NULL] Possible values for the length, number ofNAelements, number of duplicate elements, number of characters (vectorized). The options of each argument 'arg' are:NULLto not test.A single non-negative number to test for
. == arg. IfInf,. == length(x).A single negative number to test for
. == length(x) + arg.A vector of two non-negative numbers to test for
arg[1] <= . <= arg[2](Infis allowed).A vector of three or more non-negative numbers to test for
. %in% arg.A function that receives the value to test and the length of
x, and returns a singleTRUEorFALSE.
- set
[
character()|list(yes = , no = , mode = )|NULL] Test if all values ofxare in a set of allowed values. Use a list withyesand/ornoelements to defined allowed and disallowed values, withmodes"all"(allxinyes, the default),"only"(all and onlyxinyes), or"any"(anyxinyes). Set toNULLto not test.- match
[
character()|list(yes = , no = )|NULL] Test if all elements ofxmatch regular expression patterns. Pass a character vector of patterns (implicitly OR'd together), or a list withyesandnocharacter vectors of patterns. Set toNULLto not test.- sorted
[
"asc"|"desc"|NULL] Test ifxis sorted in ascending ("asc") or descending ("desc") order. Set toNULLto not test. Pair withn_dupto test for strictly sorted values.- sentinels
[
character()|NULL] Each entry in this character vector allowsxto also be some scalar sentinel below. Set toNULLto disconsider sentinels."null"forNULL."empty"for any zero-length object."na"for anyNAtype, or"na_logical"forNA,"na_integer"forNA_integer_,"na_real"forNA_real_,"na_complex"forNA_complex_, and"na_character"forNA_character_."nan"forNaN."+inf"for+Inf,"-inf"for-Inf, and"inf"for both."true"/"t"forTRUE, and"false"/"f"forFALSE.
- custom
[
function(x)|NULL] A custom function that takesxas first argument and returns a singleTRUEorFALSE. Set toNULLto not test.- perl
[
TRUE|FALSE] Should Perl-compatible regexps be used inmatch?- action
[
"abort"|"warn"|"inform"] Action to take when the test fails:"abort"to stop execution and throw an error."warning"to issue a warning and returninvisible(x)."message"to issue a message and returninvisible(x).
- env
[
environment()|call()|NULL|missing_arg()] The call to inform as the origin of the error, passed torlang::abort():An environment in the call stack or a hard-coded defused call.
NULLfor no information.missing_arg()to use the assert function itself.The default is
caller_env(), to display the function where the assertion was called.
- x_name
[
character(1)|NULL] The name of the object to use in the error message. IfNULL, the name is inferred from the expression passed tox.- short_circuit
[
TRUE|FALSE] IfTRUE, the tests results will be reported up to the first failure. Else, all tests results are reported. The former is more efficient, while the latter is more informative.- report_untested
[
TRUE|FALSE] IfTRUE, the tests that were not run due to short- circuiting will be reported as untested, else, ignored.- args_cnd
[
list()] Additional arguments passed tocli::cli_abort(),cli::cli_warn(), orcli::cli_inform(), based on the chosenaction.
Value
[
TRUE|FALSE] fortest_character().[
=x]invisible(x)forassert_character(), or aborts if the test fails.
Examples
x <- c("apple", "banana", "cherry", "banana")
args <- list(
len = c(1, 10), # Length must be between 1 and 10 (will pass)
n_na = 0, # No NA values allowed (will pass)
n_dup = 0, # No duplicate strings allowed (will fail)
n_char = c(1, 10), # Character length of each string between 1 and 10 (will pass)
set = list(no = c("date")), # Strings must not contain "date" (will pass)
match = list(
yes = "^[a-z]+$", # All elements must be lowercase letters (will pass)
no = "x" # No element can contain "x" (will pass)
),
sorted = FALSE, # Don't require vector to be sorted in alphabetical order (will pass)
sentinels = c("null"), # Allow NULL x (not the case of x)
custom = NULL # No custom predicate
)
do.call(test_character, c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(do.call(assert_character, c(list(x), args))) #> Error
#> Error in eval(expr, envir) : `x` failed `assert_character()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : must be of type "character".
#> ✔ (pass) len : length must be in range 1 to 10.
#> ✔ (pass) n_na : #of NA values must be 0.
#> ✖ (fail) n_dup : #of duplicate values must be 0. Was 1.
#> • (skip) n_char: skiped given failure.
#> • (skip) set : skiped given failure.
#> • (skip) match : skiped given failure.
#> • (skip) sorted: skiped given failure.
#>
#> ℹ See `predicater::assert_character()` and this condition's `rs_assert_error`
#> attribute for details.