inspect_character checks if an object is a character vector.
This can be useful to validate inputs in user-defined functions.
inspect_character(x, allow_nas = TRUE, warning_nas = FALSE)| x | An arbitrary object. |
|---|---|
| allow_nas | Logical value. If |
| warning_nas | Logical value. If |
inspect_character does not return any output. There are three
possible outcomes:
The call is silent if:
x is a character vector and there are no NA or NaN values in x.
x is a character vector, there are some NA or NaN values in x,
allow_nas is set to TRUE and warning_nas is set to FALSE.
An informative warning message is thrown if x is a character vector,
there are some NA or NaN values in x and both allow_nas and
warning_nas are set to TRUE.
An informative error message is thrown if:
x is not a character vector.
x is a character vector, there are some NA or NaN values in x
and allow_nas is set to FALSE.
inspect_character conducts a series of tests to check if x is a
character vector. Namely, inspect_character checks if:
x is NULL or empty.
x is an atomic vector.
The typeof x is character.
There are NA or NaN values in x.
inspect_character_match to validate character
vectors with predefined allowed values.
inspect_true_or_false to check if an object is a
non-missing logical value.
# Calls that pass silently:
x1 <- "Kass"
x2 <- c("Kass", "Raftery")
x3 <- c("Kass", "Raftery", NA)
x4 <- letters
inspect_character(x1)
inspect_character(x2)
inspect_character(x3)
inspect_character(x4)
# Call that throws an informative warning message
y <- c("Kass", "Raftery", NA)
try(inspect_character(y, warning_nas = TRUE))
#> Warning: There are NA or NaN values in y.
# Calls that throw informative error messages
try(inspect_character(y, allow_nas = FALSE))
#> Error in inspect_character(y, allow_nas = FALSE) :
#> Invalid argument: There are NA or NaN values in y.
mylist <- list(
NULL, character(0), 1,
c(1, 2), factor(c(1, 2)), list(c(1, 2)), NaN, NA
)
try(inspect_character(mylist[[1]]))
#> Error in inspect_character(mylist[[1]]) :
#> Invalid argument: mylist[[1]] is NULL.
try(inspect_character(mylist[[2]]))
#> Error in inspect_character(mylist[[2]]) :
#> Invalid argument: mylist[[2]] is empty.
try(inspect_character(mylist[[3]]))
#> Error in inspect_character(mylist[[3]]) :
#> Invalid argument: the type of mylist[[3]] must be character.
try(inspect_character(mylist[[4]]))
#> Error in inspect_character(mylist[[4]]) :
#> Invalid argument: the type of mylist[[4]] must be character.
try(inspect_character(mylist[[5]]))
#> Error in inspect_character(mylist[[5]]) :
#> Invalid argument: mylist[[5]] must be an atomic vector.
try(inspect_character(mylist[[6]]))
#> Error in inspect_character(mylist[[6]]) :
#> Invalid argument: mylist[[6]] must be an atomic vector.
try(inspect_character(mylist[[7]]))
#> Error in inspect_character(mylist[[7]]) :
#> Invalid argument: all elements of mylist[[7]] are NA or NaN.
try(inspect_character(mylist[[8]]))
#> Error in inspect_character(mylist[[8]]) :
#> Invalid argument: all elements of mylist[[8]] are NA or NaN.