Check if an argument is from some given choices or satisfies some requirement, and if not, generate an error message.

check_content(
  x,
  valid,
  name = NULL,
  general = NULL,
  specific = NULL,
  supplement = NULL,
  as_double = TRUE,
  ...
)

Arguments

x

The argument to check, which can be any object.

valid

can be

  1. a function, which takes x as argument and returns TRUE or FALSE,

  2. an expression, which contains x and evaluates to TRUE or FALSE,

  3. a string of R code, which evaluates to TRUE or FALSE, or

  4. a non-empty atomic vector, which contains the valid choices.

name

A single character which gives the argument's name. The name is used in the error message. By default, the name of the argument passed to argument x is captured automatically.

general

Optional. A single character which is used to give a general statement of the error incurred. By default, this is generated automatically.

specific

Optional. A single character which gives a detailed description of the error. By default, this is generated automatically.

supplement

Optional. A (named) character vector which gives some additional information about the error. The names are used to create bullets, see throw(). By default, this is left empty.

as_double

Optional. TRUE or FALSE which indicates if to differentiate between type double and integer. The default value is TRUE, which means integers are handled as doubles.

...

Optional. Additional arguments which can be retrieved with tryCatch().

Value

returns an invisible NULL if the argument is valid, or generates an error message.

See also

"Examples" section in check_type() for how to customize error message and how to add and retrieve additional arguments.

vignette("erify") for a gentle introduction to this package.

Examples

valid <- c(1, 2, 3)

x <- 2L
check_content(x, valid)

if (FALSE) {
# `x` must have the same type with `valid`
x <- "a"
check_content(x, valid)

# `x` must have length 1
x <- c(1, 2)
check_content(x, valid)

# differentiate between type double and integer
x <- 2L
check_content(x, valid, as_double = FALSE)

# `valid` can be a function
check_content(x, is.na, general = "`x` must be `NA`.")

# `valid` can be a string of R code
check_content(x, "is.na(x)", general = "`x` must be `NA`.")
}