Does an argument satisfy required conditions?
Check whether an argument is a logical vector of a certain length or a numeric vector in a certain range and issue an appropriate error or warning if not:
checkLogical
throws an error or returns FALSE with a warning
unless x
is a logical vector of exactly the required
length
.
checkNumeric
throws an error or returns FALSE with a warning
unless x
is either NULL or a numeric
vector of at most
length
with x
in the desired range.
checkLogicalInteger
returns a logical vector of exactly
length
unless x
is neither NULL nor logical
of
the required length
nor numeric
with x
in the
desired range.
checkLogical(x, length., warnOnly=FALSE) checkNumeric(x, lower, upper, length., integer=TRUE, unique=TRUE, inclusion=c(TRUE,TRUE), warnOnly=FALSE) checkLogicalInteger(x, length., warnOnly=FALSE)
x |
an object to be checked |
length. |
The required length for |
lower, upper |
lower and upper limits for |
integer |
logical: If true, a |
unique |
logical: TRUE if duplicates are NOT allowed in |
inclusion |
logical vector of length 2, similar to
if(inclusion[1]) (lower <= x) else (lower < x) if(inclusion[2]) (x <= upper) else (x < upper) |
warnOnly |
logical: If TRUE, violations are reported as warnings, not as errors. |
1. xName <- deparse(substitute(x)) to use in any required error or warning.
2. if(is.null(x)) handle appropriately: Return FALSE for
checkLogical
, TRUE for checkNumeric
and rep(TRUE,
length.) for checkLogicalInteger
.
3. Check class(x).
4. Check other conditions.
checkLogical
returns a logical vector of the required
length.
, unless it issues an error message.
checkNumeric
returns a numeric vector of at most length.
with all elements between lower
and upper
, and
optionally unique
, unless it issues an error message.
checkLogicalInteger
returns a logical vector of the required
length.
, unless it issues an error message.
Spencer Graves
## ## checkLogical ## checkLogical(NULL, length=3, warnOnly=TRUE) checkLogical(c(FALSE, TRUE, TRUE), length=4, warnOnly=TRUE) checkLogical(c(FALSE, TRUE, TRUE), length=3) ## ## checkNumeric ## checkNumeric(NULL, lower=1, upper=3) checkNumeric(1:3, 1, 3) checkNumeric(1:3, 1, 3, inclusion=FALSE, warnOnly=TRUE) checkNumeric(pi, 1, 4, integer=TRUE, warnOnly=TRUE) checkNumeric(c(1, 1), 1, 4, warnOnly=TRUE) checkNumeric(c(1, 1), 1, 4, unique=FALSE, warnOnly=TRUE) ## ## checkLogicalInteger ## checkLogicalInteger(NULL, 3) checkLogicalInteger(c(FALSE, TRUE), warnOnly=TRUE) checkLogicalInteger(1:2, 3) checkLogicalInteger(2, warnOnly=TRUE) checkLogicalInteger(c(2, 4), 3, warnOnly=TRUE) ## ## checkLogicalInteger names its calling function ## rather than itself as the location of error detection ## if possible ## tstFun <- function(x, length., warnOnly=FALSE){ checkLogicalInteger(x, length., warnOnly) } tstFun(NULL, 3) tstFun(4, 3, warnOnly=TRUE) tstFun2 <- function(x, length., warnOnly=FALSE){ tstFun(x, length., warnOnly) } tstFun2(4, 3, warnOnly=TRUE)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.