The assert function evaluates one or more conditions within the context of a data frame. If all conditions hold true (evaluate to TRUE), the data frame is returned (so as to seemingly work in dplyr pipelines). Otherwise, an informative error message is printed and the function stops execution. When running interactively, the function opens a View on the data.frame with a column evaluating assertion conditions (to make it easy interactively exploring what went wrong). If not interactively, it prints the rows that fail (resorting to tibble's print method).

assert(.data, ..., msg = "Assertion does not hold", na.rm = TRUE)

Arguments

.data

A data frame containing the variables used in the conditions.

...

One or more expressions to be evaluated within the data frame.

msg

A character vector specifying the message to include in the error message if the assertion fails.

na.rm

passed to all(): logical. If true NA values are removed before the result is computed.

Value

The data frame .data if all conditions are met. Otherwise, an error is thrown.

Details

This is an old function, just adapted to make it work with latests versions of pckgs. In the meantime we have used several different approaches from other packages. From the good-old stopifnot, to packages such as assertthat, assertive, assertr, the great testthat, among others I cannot remember now. Yet, we keep going back to this simple yet convinient approach. So let's put it here in efun, 'cause anyway, which we pull a few functions from here again and again in different projects.