Assertive programming follows the principles of fail fast and fail visibly (Shore 2004). It is implemented by issuing an informative error message if function arguments fail to satisfy specific criteria. This is particularly important in R because it is dynamically-typed language (Morandat et al. 2012).
checkr
is a dependency-free, pipe-friendly R package of assertive functions to check the properties of common R objects. In the case of failure, the functions, which are designed to be used in scripts and packages, issue informative error messages.
The most interesting and unique feature of checkr
is the use of objects to check the values of other objects using an elegant and expressive syntax. For example the values in the height
, name
, mass
, hair_color
and gender
columns in the starwars
data frame are checked using the syntax.
library(checkr)
check_data(dplyr::starwars, values = list(
height = c(66L, 264L),
mass = c(20,1358, NA),
hair_color = c("blond", "brown", "black", NA)), error = FALSE)
## Warning: column height of dplyr::starwars must not include missing values
## Warning: the values in column mass of dplyr::starwars must lie between 20
## and 1358
## Warning: column hair_color of dplyr::starwars can only include values
## 'black', 'blond' or 'brown'
The software archive is at https://github.com/poissonconsulting/checkr.
Morandat, Floréal, Brandon Hill, Leo Osvald, and Jan Vitek. 2012. “Evaluating the Design of the R Language.” In ECOOP 2012 – Object-Oriented Programming, edited by David Hutchison, Takeo Kanade, Josef Kittler, Jon M. Kleinberg, Friedemann Mattern, John C. Mitchell, Moni Naor, et al., 7313:104–31. Berlin, Heidelberg: Springer Berlin Heidelberg. http://link.springer.com/10.1007/978-3-642-31057-7_6.
Shore, Jim. 2004. “Fail Fast.” IEEE Software September/October:21–25. https://www.martinfowler.com/ieeeSoftware/failFast.pdf.