assert | R Documentation |
The function assert()
was inspired by stopifnot()
. It
emits a message in case of errors, which can be a helpful hint for diagnosing
the errors (stopifnot()
only prints the possibly truncated source code
of the expressions).
The infix operator %==%
is simply an alias of the
identical()
function to make it slightly easier and intuitive
to write test conditions. x %==% y
is the same as
identical(x, y)
. When it is used inside assert()
, a message
will be printed if the returned value is not TRUE
, to show the
values of the LHS (x
) and RHS (y
) via str()
,
which can be helpful for you to check why the assertion failed.
assert(fact, ...)
x %==% y
fact |
A message for the assertions when any of them fails; treated the
same way as expressions in |
... |
An R expression; see Details. |
x, y |
two R objects to be compared |
For the ...
argument, it should be a single R expression wrapped in
{}
. This expression may contain multiple sub-expressions. A
sub-expression is treated as a test condition if it is wrapped in ()
(meaning its value will be checked to see if it is a logical vector
containing any FALSE
values) , otherwise it is evaluated in the normal
way and its value will not be checked. If the value of the last
sub-expression is logical, it will also be treated as a test condition.
For assert()
, invisible NULL
if all expressions
returned TRUE
, otherwise an error is signaled and the user-provided
message is emitted. For %==%
, TRUE
or FALSE
.
The internal implementation of assert()
is different with the
stopifnot()
function in R base: (1) the custom message
fact
is emitted if an error occurs; (2) assert()
requires the
logical values to be non-empty (logical(0)
will trigger an error);
(3) if ...
contains a compound expression in {}
that returns
FALSE
(e.g., if (TRUE) {1+1; FALSE}
), the first and the last
but one line of the source code from deparse()
are printed in
the error message, otherwise the first line is printed; (4) the arguments
in ...
are evaluated sequentially, and assert()
will signal
an error upon the first failed assertion, and will ignore the rest of
assertions.
library(testit)
assert("T is bad for TRUE, and so is F for FALSE", {
T = FALSE
F = TRUE
(T != TRUE) # note the parentheses
(F != FALSE)
})
assert("A Poisson random number is non-negative", {
x = rpois(1, 10)
(x >= 0)
(x > -1) # () is optional because it's the last expression
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.