inspect_character: Validate character vectors

Description Usage Arguments Details Value See Also Examples

View source: R/inspect_.R

Description

inspect_character checks if an object is a character vector. This can be useful to validate inputs in user-defined functions.

Usage

1
inspect_character(x, allow_nas = TRUE, warning_nas = FALSE)

Arguments

x

An arbitrary object.

allow_nas

Logical value. If TRUE then NA and NaN values in x are allowed. If FALSE, execution is stopped and an error message is thrown in case there are NA or NaN values in x.

warning_nas

Logical value. If TRUE then the presence of NA or NaN values in x generates a warning message. NA and NaN values pass silently otherwise (if allow_nas is set to TRUE).

Details

inspect_character conducts a series of tests to check if x is a character vector. Namely, inspect_character checks if:

Value

inspect_character does not return any output. There are three 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
28
# 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]]))

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