Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

assert_engine

Throws an error if a condition isn't met


Description

The workhorse of the package that creates an assertion from a predicate. If a condition isn't met, then an error is thrown. This function is exported for use by package developers so that they can create their own assert functions.

Usage

assert_engine(
  predicate,
  ...,
  msg = "The assertion failed.",
  what = c("all", "any"),
  na_ignore = FALSE,
  severity = c("stop", "warning", "message", "none")
)

Arguments

predicate

Function that returns a logical value (possibly a vector).

...

Passed to the predicate function.

msg

The error message, in the event of failure.

what

Either 'all' or 'any', to reduce vectorised tests to a single value.

na_ignore

A logical value. If FALSE, NA values cause an error; otherwise they do not. Like na.rm in many stats package functions, except that the position of the failing values does not change.

severity

How severe should the consequences of the assertion be? Either "stop", "warning", "message", or "none".

Value

FALSE with the attribute message, as provided in the input.

Note

Missing values are considered as FALSE for the purposes of whether or not an error is thrown.

Examples

# Basic usage is like do.call; pass a predicate and the arguments to it.
dont_stop(assert_engine(is_true, c(TRUE, FALSE, NA)))

# Customise the error message
dont_stop(
  assert_engine(is_true, c(TRUE, FALSE, NA), msg = "Not everything is true")
)

# Only fail when no values match the predicate's conditions
dont_stop(assert_engine(is_true, logical(3), what = "any"))

# You can use base predicates, but the error message isn't as informative
dont_stop(assert_engine(is.matrix, 1:5))

# Reduce the severity of failure
assert_engine(is_true, c(TRUE, FALSE, NA), severity = "message")

assertive.base

A Lightweight Core of the 'assertive' Package

v0.0-9
GPL (>= 3)
Authors
Richard Cotton [aut, cre], Sunkyu Choi [trl], Ivanka Skakun [trl], Gergely Dar<c3><b3>czi [trl], Anton Antonov [trl], Hisham Ben Hamidane [trl], Anja Billing [trl], Aditya Bhagwat [trl], Rasmus B<c3><a5><c3><a5>th [trl], Mine Cetinkaya-Rundel [trl], Aspasia Chatziefthymiou [trl]
Initial release
2021-02-07

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.