checkmate: Fast Argument Checks for Defensive R Programming

Dynamically typed programming languages like R allow programmers to write generic, flexible and concise code and to interact with the language using an interactive Read-eval-print-loop (REPL). However, this flexibility has its price: As the R interpreter has no information about the expected variable type, many base functions automatically convert the input instead of raising an exception. Unfortunately, this frequently leads to runtime errors deeper down the call stack which obfuscates the original problem and renders debugging challenging. Even worse, unwanted conver sions can remain undetected and skew or invalidate the results of a statistical analysis. As a resort, assertions can be employed to detect unexpected input during runtime and to signal understandable and traceable errors. The package checkmate provides a plethora of functions to check the type and related properties of the most frequently used R objects and variable types. The package is mostly written in C to avoid any unnecessary performance overhead. Thus, the programmer can conveniently write concise, well-tested assertions which outperforms custom R code for many applications. Fur thermore, checkmate simplifies writing unit tests using the framework testthat (Wickham, 2011) by extending it with plenty of additional expectation functions, and registered C routines are available for package developers to perform assertions on arbitrary SEXPs (internal data structure for R objects implemented as struct in C) in compiled code.

Michel Lang

CRAN packages used

checkmate, assertthat, assertive, assertive.numbers, assertive.sets, assertr, magrittr, dplyr, testthat, data.table, tibble, microbenchmark, mlr, BatchJobs, batchtools

CRAN Task Views implied by cited packages

HighPerformanceComputing, Finance, MachineLearning, WebTechnologies


Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".


For attribution, please cite this work as

Lang, "The R Journal: checkmate: Fast Argument Checks for Defensive R Programming", The R Journal, 2017

BibTeX citation

  author = {Lang, Michel},
  title = {The R Journal: checkmate: Fast Argument Checks for Defensive R Programming},
  journal = {The R Journal},
  year = {2017},
  note = {},
  doi = {10.32614/RJ-2017-028},
  volume = {9},
  issue = {1},
  issn = {2073-4859},
  pages = {437-445}