[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  stopifnot: Ensure the Truth of R Expressions

##  Description

If any of the expressions (in ` … ` or ` exprs ` ) are not ` [ all
](/link/all?package=base&version=3.6.2) ` ` TRUE ` , ` [ stop
](/link/stop?package=base&version=3.6.2) ` is called, producing an error
message indicating the _first_ expression which was not ( ` [ all
](/link/all?package=base&version=3.6.2) ` ) true.

##  Usage

    
    
    stopifnot(…, exprs, local = TRUE)

##  Arguments

…, exprs

any number of (typically but not necessarily ` [ logical
](/link/logical?package=base&version=3.6.2) ` )  R  expressions, which should
each evaluate to (a logical vector of all) ` [ TRUE
](/link/TRUE?package=base&version=3.6.2) ` . Use _either_ ` … ` _or_ ` exprs `
, the latter typically an unevaluated expression of the form

    
    
    {
       expr1
       expr2
       ....
    }

local

(only when ` exprs ` is used:) indicates the ` [ environment
](/link/environment?package=base&version=3.6.2) ` in which the expressions
should be evaluated; by default the one where ` stopifnot() ` has been called
from.

##  Value

( ` [ NULL ](/link/NULL?package=base&version=3.6.2) ` if all statements in ` …
` are ` TRUE ` .)

##  Details

This function is intended for use in regression tests or also argument
checking of functions, in particular to make them easier to read.

` stopifnot(A, B) ` or equivalently ` stopifnot(exprs= {A ; B}) ` are
conceptually equivalent to

    
    
     { if(any(is.na(A)) || !all(A)) stop(...);
       if(any(is.na(B)) || !all(B)) stop(...) }

Since  R  version 3.6.0, ` stopifnot() ` no longer handles potential errors or
warnings (by ` [ tryCatch ](/link/tryCatch?package=base&version=3.6.2) () `
etc) for each single expression  and may use ` [ sys.call
](/link/sys.call?package=base&version=3.6.2) (<n>) ` to get a meaningful and
short error message in case an expression did not evaluate to all TRUE. This
provides considerably less overhead.

Since  R  version 3.5.0, expressions _are_ evaluated sequentially, and hence
evaluation stops as soon as there is a “non-TRUE”, as indicated by the above
conceptual equivalence statement. Further, when such an expression signals an
error or ` [ warning ](/link/warning?package=base&version=3.6.2) ` , the
message produced no longer contains the full ` stopifnot ` call, but just the
erroneous expression.

Also, since  R  version 3.5.0, ` stopifnot(exprs = { ... }) ` can be used
alternatively and may be preferable in the case of several expressions, as
they are more conveniently evaluated interactively (“no extraneous ` , ` ”).

Since  R  version 3.4.0, when an expression (from ` … ` ) is not true _and_ is
a call to ` [ all.equal ](/link/all.equal?package=base&version=3.6.2) ` , the
error message will report the (first part of the) differences reported by ` [
all.equal ](/link/all.equal?package=base&version=3.6.2) (*) ` .

##  See Also

` [ stop ](/link/stop?package=base&version=3.6.2) ` , ` [ warning
](/link/warning?package=base&version=3.6.2) ` ; ` [ assertCondition
](/link/assertCondition?package=base&version=3.6.2) ` in package  tools
complements ` stopifnot() ` for testing warnings and errors.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fstopifnot&utm_source=r-docs&utm_medium=docs&utm_term=stopifnot&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    stopifnot(1 == 1, all.equal(pi, 3.14159265), 1 < 2) # all TRUE
    
    m <- matrix(c(1,3,3,1), 2, 2)
    stopifnot(m == t(m), diag(m) == rep(1, 2)) # all(.) |=>  TRUE
    
    op <- options(error = expression(NULL))
    # "disabling stop(.)"  << Use with CARE! >>
    
    stopifnot(all.equal(pi, 3.141593),  2 < 2, all(1:10 < 12)