Learn R Programming

BBmisc (version 1.1-125)

checkArg: Check for a function argument.

Description

Throws exception if checks are not passed. Note that argument is evaluated when checked.

Usage

checkArg(x, cl, len, min.len, choices, subset,
    lower = NA, upper = NA, na.ok = TRUE, formals)

Arguments

x
[any] Argument.
cl
[character(1)] Class that argument must have. Checked with is.
len
[integer(1)] Length that argument must have. Not checked if not passed, which is the default.
min.len
[integer(1)] Minimal length that argument must have. Not checked if not passed, which is the default.
choices
[any] Discrete number of choices, expressed by a vector of R objects. If passed, argument must be identical to one of these and nothing else is checked.
subset
[any] Discrete number of choices, expressed by a vector of R objects. If passed, argument must be identical to a subset of these and nothing else is checked.
lower
[numeric(1)] Lower bound for numeric vector arguments. Default is NA, which means not required.
upper
[numeric(1)] Upper bound for numeric vector arguments. Default is NA, which means not required.
na.ok
[logical(1)] Is it ok if a vector argument contains NAs? Default is TRUE.
formals
[character] Is it ok if a vector argument contains NAs? Default is TRUE.

Value

  • Nothing.

Examples

Run this code
x <- 1L
checkArg(x, "integer", len=1, na.ok=FALSE, upper=3L)
x <- as.integer(NA)
checkArg(x, "integer", len=1, na.ok=TRUE)
x <- c("foo", "bar")
checkArg(x, "character")
x <- "foo"
checkArg(x, choices=c("foo", "bar"))
x <- c("foo", "bar")
checkArg(x, subset=c("foo", "bar"))
fun <- function(foo, bar)
checkArg(fun, formals=c("foo", "bar"))

Run the code above in your browser using DataLab