Learn R Programming

checkmate (version 1.0)

qassert: Quick argument checks on (builtin) R types

Description

The provided functions parse rules which allow to express some of the most frequent argument checks by typing just a few letters.

Usage

qassert(x, rules, .var.name)

qtest(x, rules)

Arguments

x
[ANY] Object the check.
rules
[character] Set of rules. See details.
.var.name
[logical(1)] Argument name to print in error message. If missing, the name of x will be retrieved via substitute.

Value

  • [logical(1)]: TRUE on success, FALSE (or a thrown exception) otherwise.

item

  • Length definition. This can be one of rl{ [*] any length, [?] length of zero or one, [+] length of at least one, or [0-9]+ exact length specified as integer. } Preceeding the exact length with one of the comparison operators =/==, <, <=< code="">, >= or > is also supported.
  • Range check as two numbers separated by a comma, enclosed by square brackets (endpoint included) or parentheses (endpoint excluded). For example, [0, 3) would trigger the check all(x >= 0 & x < 3). Endpoints may be omitted which is the equivalent of an infinite endpoint. By definition [0,] contains Inf, while [0,) does not. The same holds for the left (lower) endpoint and -Inf. E.g., the rule N1() checks for a single finite numeric which is not NA, while N1[) would allow -Inf.

Details

qassert throws an R exception if object x does not comply to at least one of the rules and returns TRUE otherwise. qtest behaves the same way but returns FALSE if none of the rules comply.

The rule is specified in up to three parts.

  1. Class and missingness check. The first letter is an abbreviation for the class. If it is provided uppercase, missing values are prohibited. Supported abbreviations:rl{[bB] Bool / logical. [iI] Integer. [xX] Integerish (numeric convertible to integer, seecheckIntegerish). [rR] Real / double. [cC] Complex. [nN] Numeric (integer or double). [sS] String / character. [aA] Atomic. [vV] Atomic vector (seecheckAtomicVector). [lL] List. Missingness is defined asNULLelement. [mM] Matrix. [dD] Data.frame. Missingness is checked recursively on columns. [e] Environment. [f] Function. [0] NULL. [*] placeholder to allow any type.
Note that the check for missingness does not distinguish between NaN and NA. Infinite values are not treated as missing, but can be catched using boundary checks (part 3).

See Also

qtestr and qassertr for efficient checks of list elements and data frame columns.

Examples

Run this code
# logical of length 1
 qtest(NA, "b1")

 # logical of length 1, NA not allowed
 qtest(NA, "B1")

 # logical of length 0 or 1, NA not allowed
 qtest(TRUE, "B?")

 # numeric with length > 0
 qtest(runif(10), "n+")

 # integer with length > 0, NAs not allowed, all integers >= 0 and < Inf
 qtest(1:3, "I+[0,)")

 # either an emtpy list or a character vector with <=5 elements
 qtest(1, c("l0", "s<=5"))
 # data frame with at least one column, no NA in any column
 qtest(iris, "D+")

Run the code above in your browser using DataLab