inspect_character_match: Validate character values

Description Usage Arguments Details Value See Also Examples

View source: R/inspect_.R

Description

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.

Usage

1
inspect_character_match(x, allowed, case_sensitive = FALSE)

Arguments

x

An arbitrary object.

allowed

A character vector.

case_sensitive

A non-missing logical value.

Details

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:

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.

Value

inspect_character_match does not return any output. There are two possible outcomes:

See Also

Examples

 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"))

inspector documentation built on June 18, 2021, 1:06 a.m.