assert: Flexible Error Assertion

View source: R/assert.R

assertR Documentation

Flexible Error Assertion

Description

assert_all() and assert_any() are functions that require all or any of the expressions in ... to be TRUE. They are more flexible versions of stopifnot() that also provide the conditioning system benefits of abort(). They are powered (and generalized) by assert().

Usage

assert(
  ...,
  message = NULL,
  class = NULL,
  data = NULL,
  trace = NULL,
  parent = NULL,
  reduce = base::all
)

assert_all(
  ...,
  message = NULL,
  class = NULL,
  data = NULL,
  trace = NULL,
  parent = NULL
)

assert_any(
  ...,
  message = NULL,
  class = NULL,
  data = NULL,
  trace = NULL,
  parent = NULL
)

Arguments

...

Unnamed expressions that describe the conditions to be tested

message

The message to display. Character vectors are formatted with format_error_bullets(). The first element defines a message header and the rest of the vector defines bullets. Bullets named i and x define info and error bullets respectively, with special Unicode and colour formatting applied if possible.

If a message is not supplied, it is expected that the message is generated lazily through conditionMessage(). In that case, class must be supplied. Only inform() allows empty messages as it is occasionally useful to build user output incrementally.

class

Subclass of the condition. This allows your users to selectively handle the conditions signalled by your functions.

data

Additional data to be stored in the condition object

trace

A trace object created by trace_back().

parent

A parent condition object created by abort().

reduce

A function that takes a vector of logical values as an argument and returns a single logical value. reduce is given the results of the expressions in ... and decides whether the assertion passed or failed. The default is all() (i.e. all expressions must be TRUE), but any function that meets the criteria above is acceptable.

Details

assert() evaluates logical expressions and throws an error if the conditions specified by reduce are not met. It allows any combination of logical conditions (not just "all TRUE" or "any TRUE") and provides the conditioning system benefits of abort().

Value

TRUE if the assertions evaluate to TRUE, otherwise an error condition


jesse-smith/coviData documentation built on Jan. 14, 2023, 11:08 a.m.