fda (version 6.1.8)

checkLogicalInteger: Does an argument satisfy required conditions?

Description

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.

Usage

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)

Value

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.

Arguments

x

an object to be checked

length.

The required length for x if logical and not NULL or the maximum length if numeric.

lower, upper

lower and upper limits for x.

integer

logical: If true, a numeric x must be integer.

unique

logical: TRUE if duplicates are NOT allowed in x.

inclusion

logical vector of length 2, similar to link[ifultools]{checkRange}:

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.

Author

Spencer Graves

Details

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.

References

Ramsay, James O., Hooker, Giles, and Graves, Spencer (2009), Functional data analysis with R and Matlab, Springer, New York.

Ramsay, James O., and Silverman, Bernard W. (2005), Functional Data Analysis, 2nd ed., Springer, New York.

Ramsay, James O., and Silverman, Bernard W. (2002), Applied Functional Data Analysis, Springer, New York.

Examples

Run this code
##
## 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)

Run the code above in your browser using DataLab