Learn R Programming

BBmisc (version 1.10)

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, s4 = FALSE, len, min.len, max.len, choices, subset, lower = NA, upper = NA, na.ok = TRUE, formals)

Arguments

x
[any] Argument.
cl
[character] Class that argument must “inherit” from. If multiple classes are given, x must “inherit” from at least one of these. See also argument s4.
s4
[logical(1)] If TRUE, use is for checking class cl, otherwise use inherits, which implies that only S3 classes are correctly checked. This is done for speed reasons as calling is is pretty slow. Default is FALSE.
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.
max.len
[integer(1)] Maximal 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] If this is passed, x must be a function. It is then checked that formals are the names of the (first) formal arguments in the signature of x. Meaning checkArg(function(a, b), formals = "a") is ok. Default is missing.

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