Check if an argument is a single natural number, and if not, generate an error message.

Can be used to check indices, for example.

check_n(
  x,
  name = NULL,
  general = NULL,
  specific = NULL,
  supplement = NULL,
  zero = FALSE,
  ...
)

is_n(x, zero = FALSE)

Arguments

x

The argument to check, which can be any object.

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.

zero

Optional. TRUE or FALSE which indicates if zero is acceptable. The default value is FALSE.

...

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

Value

check_n() returns an invisible NULL if the argument is valid, or it generates an error message.

is_n() returns TRUE or FALSE.

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

x <- 1
check_n(x)

x <- 1L
check_n(x)

sapply(c(1, 2.1, 0, Inf, NA, -9), is_n)
#> [1]  TRUE FALSE FALSE FALSE FALSE FALSE

if (FALSE) {
# `x` must be a numeric
x <- "1"
check_n(x)

# `x` must have length 1
x <- 1:2
check_n(x)

# `x` must not be `NA`
x <- NA_integer_
check_n(x)

# `x` must be larger than 0
x <- -1
check_n(x)

# `x` must be an integer in a mathematical sense
x <- 1.1
check_n(x)

# make `0` acceptable
x <- 0
check_n(x)
check_n(x, zero = TRUE)
}