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.
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
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_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
to validate character vectors
with arbitrary allowed values.
inspect_true_or_false
to check if an object is a
non-missing logical value.
# 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")))
#> Error in inspect_character_match(y1, allowed = c("Kass", "Raftery")) :
#> Invalid argument: y1 = 'kasss' is not allowed.
try(inspect_character_match(y2,
allowed = c("Kass", "Raftery"),
case_sensitive = TRUE
))
#> Error in inspect_character_match(y2, allowed = c("Kass", "Raftery"), case_sensitive = TRUE) :
#> Invalid argument: y2 = 'kass' is not allowed.
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"))
#> Error in inspect_character_match(mylist[[1]], "abc") :
#> Invalid argument: mylist[[1]] is NULL.
try(inspect_character_match(mylist[[2]], "abc"))
#> Error in inspect_character_match(mylist[[2]], "abc") :
#> Invalid argument: mylist[[2]] must be an atomic vector of length 1.
try(inspect_character_match(mylist[[3]], "abc"))
#> Error in inspect_character_match(mylist[[3]], "abc") :
#> Invalid argument: mylist[[3]] must be an atomic vector of length 1.
try(inspect_character_match(mylist[[4]], "abc"))
#> Error in inspect_character_match(mylist[[4]], "abc") :
#> Invalid argument: mylist[[4]] must be an atomic vector of length 1.
try(inspect_character_match(mylist[[5]], "abc"))
#> Error in inspect_character_match(mylist[[5]], "abc") :
#> Invalid argument: mylist[[5]] = 'ab' is not allowed.
try(inspect_character_match(mylist[[6]], "abc"))
#> Error in inspect_character_match(mylist[[6]], "abc") :
#> Invalid argument: mylist[[6]] must be an atomic vector of length 1.
try(inspect_character_match(mylist[[7]], "abc"))
#> Error in inspect_character_match(mylist[[7]], "abc") :
#> Invalid argument: mylist[[7]] must be an atomic vector of length 1.
try(inspect_character_match(mylist[[8]], "abc"))
#> Error in inspect_character_match(mylist[[8]], "abc") :
#> Invalid argument: mylist[[8]] is NA or NaN.
try(inspect_character_match(mylist[[9]], "abc"))
#> Error in inspect_character_match(mylist[[9]], "abc") :
#> Invalid argument: mylist[[9]] is NA or NaN.