Learn R Programming

gtools (version 2.3.1)

assert: Generate an error if an expression is not true.

Description

Generate an error if an expression is not true.

Usage

assert(FLAG)

Arguments

FLAG
Expression that should evaluate to a boolean vector

Value

  • None. Evaluated only for its side effect.

Details

Assert generate an error if its aregument does not evaluate to boolean (vector) containing only TRUE values. This is useful for defensinve programming as it provides a mechanism for checking that certain facts, the 'assertions', do in fact hold. Checking of 'assertions' is an important tool in the development of robust program code.

See Also

stop, warning

Examples

Run this code
## Trivial example
posSqrt <- function(x)
  {
    assert(x>=0)
    sqrt(x)
  }

posSqrt(1:10) # works fine, no messages
posSqrt(-5:5) # generates an error, since the asssertion is not met

Run the code above in your browser using DataLab