assert: Assertions and Argument Validation

Description Usage Arguments Value Examples

View source: R/assert.R

Description

Assert that each of the provided expression is true. Otherwise stop execution and return a description of each failed assertion.

Usage

1
assert(..., msg = "", stop = TRUE)

Arguments

...

List of logical assertions.

msg

Message to print alongside the list of failed assertions (if any).

stop

Whether execution should be stopped on failed assertions. If set to FALSE, then only a warning is issued. Default is TRUE.

Value

Nothing if all assertions pass. Otherwise throws an error describing which

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
attach(ChickWeight)

# Passing assertions
assert(is.numeric(weight),
       all(weight > 0))

# Failing assertions
if (interactive()) {
  assert(all(Diet > 0),
         is.numeric(Times))
}

# Validating function arguments
sum <- function(a, b) {
  assert(is.numeric(a),
         is.numeric(b),
         length(a) == length(b))

  return(a+b)
}

sum(2, 2)

if (interactive()) {
  sum("1", 2)
  sum(1, c(1,2))
  sum(1, x)
}

assert documentation built on Nov. 29, 2020, 1:07 a.m.

Related to assert in assert...