| inspect_character {inspector} | R Documentation |
Validate character vectors
Description
inspect_character checks if an object is a character vector.
This can be useful to validate inputs in user-defined functions.
Usage
inspect_character(x, allow_nas = TRUE, warning_nas = FALSE)
Arguments
x |
An arbitrary object. |
allow_nas |
Logical value. If |
warning_nas |
Logical value. If |
Details
inspect_character conducts a series of tests to check if x is a
character vector. Namely, inspect_character checks if:
-
xisNULLor empty. -
xis an atomic vector. The
typeofxis character.There are
NAorNaNvalues inx.
Value
inspect_character does not return any output. There are three
possible outcomes:
The call is silent if:
-
xis a character vector and there are noNAorNaNvalues inx. -
xis a character vector, there are someNAorNaNvalues inx,allow_nasis set toTRUEandwarning_nasis set toFALSE.
-
An informative warning message is thrown if
xis a character vector, there are someNAorNaNvalues inxand bothallow_nasandwarning_nasare set toTRUE.An informative error message is thrown if:
-
xis not a character vector. -
xis a character vector, there are someNAorNaNvalues inxandallow_nasis set toFALSE.
-
See Also
-
inspect_character_matchto validate character vectors with predefined allowed values. -
inspect_true_or_falseto check if an object is a non-missing logical value.
Examples
# 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))
# Calls that throw informative error messages
try(inspect_character(y, allow_nas = FALSE))
mylist <- list(
NULL, character(0), 1,
c(1, 2), factor(c(1, 2)), list(c(1, 2)), NaN, NA
)
try(inspect_character(mylist[[1]]))
try(inspect_character(mylist[[2]]))
try(inspect_character(mylist[[3]]))
try(inspect_character(mylist[[4]]))
try(inspect_character(mylist[[5]]))
try(inspect_character(mylist[[6]]))
try(inspect_character(mylist[[7]]))
try(inspect_character(mylist[[8]]))