Validation - Names attribute
test_names.RdTest if an object's names (or character vector of names) satisfies some conditions.
test_names() is the predicate test, while assert_names() validates
its input, aborting if it fails the test.
Hint: use sentinels = c("null") to allow no (NULL) names, and empty = TRUE to always pass the test if the underlying vector x is empty.
Usage
test_names(
x,
n_na = NULL,
n_empty = NULL,
n_dup = NULL,
n_invalid = NULL,
set = NULL,
tests_char = NULL,
how = "names",
empty = NULL,
sentinels = NULL,
custom = NULL
)
assert_names(
x,
n_na = NULL,
n_empty = NULL,
n_dup = NULL,
n_invalid = NULL,
set = NULL,
tests_char = NULL,
how = "names",
empty = NULL,
sentinels = NULL,
custom = NULL,
action = "abort",
env = caller_env(),
x_name = NULL,
short_circuit = TRUE,
report_untested = TRUE,
args_cnd = list()
)Arguments
- x
[
any] An object to get names from, or a character vector of names to test.- n_na, n_dup, n_empty, n_invalid
[
numeric()|\(){}|NULL] Possible values for the number ofNAelements, number of duplicate elements, number of elements with zero length, number of non-syntactic elements. 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.- tests_char
[
list] A list of additional arguments passed totest_character().- how
[
"names"|"x"|"attr"|"colnames"|"row.names"|integer(1)] How to extract names fromx:"x"forxdirectly;"names"fornames(x);"attr"forattr(x, "names");"colnames"forcolnames(x);"row.names"forattr(x, "row.names"); or a positive integer fordimnames(x)[[how]].- empty
[
TRUE|NULL] Whether to early pass the test if the underlying vectorxis empty.- 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.- 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_names().[
=x]invisible(x)forassert_names(), or aborts if the test fails.
Examples
x <- rlang::set_names(1:6, c("a", "b", "c", NA, "", ""))
args <- list(
n_na = 0, # No NA names (will fail)
n_dup = NULL, # Don't test for duplicates
n_empty = c(0, -1), # Between 0 and length(x) - 1 empty names (will pass)
n_invalid = c(0, Inf), # Between 0 and Inf invalid names (same as not testing)
set = list(yes = c("a", "b"), no = c("d", "e")),
# Names must be only "a" or "b", and not "d" nor "e" (will fail)
how = "names", # Use `names(x)` as the names vector to test
empty = NULL, # Don't allow empty `x` (will pass)
sentinels = c("null"), # Allow `NULL` names (not the case of x)
custom = \(x) isTRUE(all(nchar(x) == 1))
# All names must be a single character (will fail)
)
do.call(test_names, c(list(x), args)) #> FALSE (not all tests passed)
#> [1] FALSE
try(do.call(assert_names, c(list(x), args, short_circuit = FALSE))) #> Error
#> Error in eval(expr, envir) : `x` failed `assert_names()`:
#> ✔ (pass) sentinels: no sentinel values allowed.
#> ✔ (pass) type : names must be of type "character".
#> ✖ (fail) n_na : #of NA values must be 0. Was 1.
#> ✔ (pass) n_empty: #of empty string names must be in range 0 to -1.
#> ✔ (pass) n_invalid: #of syntactically invalid names must be in range 0 to Inf.
#> ✖ (fail) set : must be in a custom set. Was not.
#> ✖ (fail) custom: must pass a custom test. Did not.
#>
#> ℹ See `predicater::assert_names()` and this condition's `rs_assert_error`
#> attribute for details.