Description Usage Arguments Details Value See Also Examples
inspect_character_match
checks if an object is a character
vector of length
1 that belongs to a set of allowed
values. This can be useful to validate inputs in user-defined functions.
1 | inspect_character_match(x, allowed, case_sensitive = FALSE)
|
x |
An arbitrary object. |
allowed |
A character vector. |
case_sensitive |
A non-missing logical value. |
inspect_character_match
conducts a series of tests to check if x
is a character vector of length
1 whose value belongs to
the set of allowed values. Namely, inspect_character_match
checks if:
x
is NULL
or empty.
x
is an atomic vector of length
1.
The typeof
x
is character.
x
is NA
or NaN
.
x
is one of the allowed values (as specified in the allowed
argument).
By default, the comparison of x
with allowed
is not case sensitive. If
you only want case sensitive matches of x
to allowed
set case_sensitive
to TRUE
.
inspect_character_match
does not return any output. There are two
possible outcomes:
The call is silent if x
is a character vector of
length
1 whose value belongs to the set of allowed
values.
An informative error message is thrown otherwise.
inspect_character
to validate character vectors
with arbitrary allowed values.
inspect_true_or_false
to check if an object is a
non-missing logical value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Calls that pass silently:
x1 <- "Kass"
x2 <- "kass"
inspect_character_match(x1, allowed = c("Kass", "Raftery"))
inspect_character_match(x2, allowed = c("Kass", "Raftery"))
# Calls that throw informative error messages:
y1 <- "kasss"
y2 <- "kass"
try(inspect_character_match(y1, allowed = c("Kass", "Raftery")))
try(inspect_character_match(y2,
allowed = c("Kass", "Raftery"),
case_sensitive = TRUE
))
mylist <- list(
NULL, character(0), c("abc", "abcd"),
c("abc", "abc"), "ab", list("abc"), factor("abc"), NaN, NA
)
try(inspect_character_match(mylist[[1]], "abc"))
try(inspect_character_match(mylist[[2]], "abc"))
try(inspect_character_match(mylist[[3]], "abc"))
try(inspect_character_match(mylist[[4]], "abc"))
try(inspect_character_match(mylist[[5]], "abc"))
try(inspect_character_match(mylist[[6]], "abc"))
try(inspect_character_match(mylist[[7]], "abc"))
try(inspect_character_match(mylist[[8]], "abc"))
try(inspect_character_match(mylist[[9]], "abc"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.