Check if each item of an argument has valid length, and if not, generate an error message.

check_lengths(
  x,
  valid,
  name = NULL,
  general = NULL,
  specific = NULL,
  supplement = NULL,
  interval = NULL,
  ...
)

Arguments

x

The argument to check, which must be a list.

valid

A numeric vector which contains non-negative integers and NA, used with argument interval to indicate the valid lengths.

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. glue::glue() syntax can be used, see "Examples" section. 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.

interval

Optional. TRUE or FALSE which indicates if argument valid is interpreted as an interval or as single lengths. For example, c(1, 10) is interpreted as "larger than 1 and smaller than 10" if interval is TRUE, but as "1 or 10" if FALSE. NA can be used in valid when treated as interval. For example, c(0, NA) means "larger than 0". By default, interval is inferred from valid. For example, if valid has length unequal to 2, it's treated as single lengths.

...

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

if (FALSE) {
x <- list(1, c(1, 2), c(1, 2, 3))

check_lengths(x, c(1, NA))

specific = "Item {i} has length {feature}."
check_lengths(x, c(1, NA), specific = specific)
}