Fast and versatile argument checks for R.
Ever used an R function that produced a not-very-helpful error message, just to discover after minutes of debugging that you simply passed a wrong argument?
Blaming the laziness of the package author for not doing such standard checks (in a dynamically typed language such as R) is at least partially unfair, as R makes theses types of checks cumbersome and annoying. Well, that's how it was in the past.
Enter checkmate.
Virtually every standard type of user error when passing arguments into function can be caught with a simple, readable line which produces an informative error message in case. A substantial part of the package was written in C to minimize any worries about execution time overhead.
Here is an overview of the most useful functions for argument checking:
- checkNull
- checkFlag
- checkNumber
- checkCount
- checkInt
- checkString
- checkClass
- checkScalar
- checkScalarNA
- asInt
- asCount
What can be checked: Simple, non-NA scalars and objects of a specific class.
What can be checked: Choices like "A", "B" or "C", subset of those or set equality.
- checkLogical
- checkNumeric
- checkInteger
- checkIntegerish
- checkComplex
- checkCharacter
- checkFactor
- checkVector
- checkAtomic
- checkAtomicVector
- asInteger
What can be checked: Length, upper and lower bounds, NAs, duplicates, names.
What can be checked: Storage mode (numeric, char, etc.), number of rows, cols, NAs, names.
What can be checked: length, element type, names.
What can be checked: Arguments and ordered arguments.
What can be checked: Path exists, is accessible.
Perform multiple checks at once and throw an assertion if all checks fail.
These functions allow a special syntax to define argument checks using a special pattern.
E.g., qassert(x, "I+")
asserts that x
is an integer vector with at least one element and no missing values.
This provide a completely alternative mini-language (or style) how to perform argument checks.
You choose what you like best.