| Line 99... |
Line 99... |
| 99 |
\seealso{\code{\link{stop}}, \code{\link{warning}};
|
99 |
\seealso{\code{\link{stop}}, \code{\link{warning}};
|
| 100 |
\code{\link{assertCondition}} in package \pkg{tools} complements
|
100 |
\code{\link{assertCondition}} in package \pkg{tools} complements
|
| 101 |
\code{stopifnot()} for testing warnings and errors.
|
101 |
\code{stopifnot()} for testing warnings and errors.
|
| 102 |
}
|
102 |
}
|
| 103 |
\examples{
|
103 |
\examples{
|
| 104 |
## NB: These examples should rather be run one by one;
|
104 |
## NB: Some of these examples are expected to produce an error. To
|
| 105 |
## example(stopifnot) is not a good idea: will run to the first error
|
105 |
## prevent them from terminating a run with example() they are
|
| - |
|
106 |
## piped into a call to try().
|
| 106 |
|
107 |
|
| 107 |
stopifnot(1 == 1, all.equal(pi, 3.14159265), 1 < 2) # all TRUE
|
108 |
stopifnot(1 == 1, all.equal(pi, 3.14159265), 1 < 2) # all TRUE
|
| 108 |
|
109 |
|
| 109 |
m <- matrix(c(1,3,3,1), 2, 2)
|
110 |
m <- matrix(c(1,3,3,1), 2, 2)
|
| 110 |
stopifnot(m == t(m), diag(m) == rep(1, 2)) # all(.) |=> TRUE
|
111 |
stopifnot(m == t(m), diag(m) == rep(1, 2)) # all(.) |=> TRUE
|
| 111 |
|
112 |
|
| 112 |
\dontshow{if(!interactive()) op <- options(error = expression(NULL))
|
- |
|
| 113 |
# only in batch mode: after error / stop(.), evaluation will continue
|
- |
|
| 114 |
}
|
- |
|
| 115 |
stopifnot(length(10)) # gives an error: '1' is *not* TRUE
|
113 |
stopifnot(length(10)) |> try() # gives an error: '1' is *not* TRUE
|
| 116 |
## even when if(1) "ok" works
|
114 |
## even when if(1) "ok" works
|
| 117 |
|
115 |
|
| 118 |
stopifnot(all.equal(pi, 3.141593), 2 < 2, (1:10 < 12), "a" < "b")
|
116 |
stopifnot(all.equal(pi, 3.141593), 2 < 2, (1:10 < 12), "a" < "b") |> try()
|
| 119 |
## More convenient for interactive "line by line" evaluation:
|
117 |
## More convenient for interactive "line by line" evaluation:
|
| 120 |
stopifnot(exprs = {
|
118 |
stopifnot(exprs = {
|
| 121 |
all.equal(pi, 3.1415927)
|
119 |
all.equal(pi, 3.1415927)
|
| 122 |
2 < 2
|
120 |
2 < 2
|
| 123 |
1:10 < 12
|
121 |
1:10 < 12
|
| 124 |
"a" < "b"
|
122 |
"a" < "b"
|
| 125 |
})
|
123 |
}) |> try()
|
| 126 |
|
124 |
|
| 127 |
eObj <- expression(2 < 3, 3 <= 3:6, 1:10 < 2)
|
125 |
eObj <- expression(2 < 3, 3 <= 3:6, 1:10 < 2)
|
| 128 |
stopifnot(exprObject = eObj)
|
126 |
stopifnot(exprObject = eObj) |> try()
|
| 129 |
stopifnot(exprObject = quote(3 == 3))
|
127 |
stopifnot(exprObject = quote(3 == 3))
|
| 130 |
stopifnot(exprObject = TRUE)
|
128 |
stopifnot(exprObject = TRUE)
|
| 131 |
|
129 |
|
| 132 |
|
130 |
|
| 133 |
# long all.equal() error messages are abbreviated:
|
131 |
# long all.equal() error messages are abbreviated:
|
| 134 |
stopifnot(all.equal(rep(list(pi),4), list(3.1, 3.14, 3.141, 3.1415)))
|
132 |
stopifnot(all.equal(rep(list(pi),4), list(3.1, 3.14, 3.141, 3.1415))) |> try()
|
| 135 |
|
133 |
|
| 136 |
# The default error message can be overridden to be more informative:
|
134 |
# The default error message can be overridden to be more informative:
|
| 137 |
m[1,2] <- 12
|
135 |
m[1,2] <- 12
|
| 138 |
stopifnot("m must be symmetric"= m == t(m))
|
136 |
stopifnot("m must be symmetric"= m == t(m)) |> try()
|
| 139 |
#=> Error: m must be symmetric
|
137 |
#=> Error: m must be symmetric
|
| 140 |
|
138 |
|
| 141 |
\dontshow{if(!interactive()) options(op) # revert to previous error handler
|
- |
|
| 142 |
}
|
- |
|
| 143 |
##' warnifnot(): a "only-warning" version of stopifnot()
|
139 |
##' warnifnot(): a "only-warning" version of stopifnot()
|
| 144 |
##' {Yes, learn how to use do.call(substitute, ...) in a powerful manner !!}
|
140 |
##' {Yes, learn how to use do.call(substitute, ...) in a powerful manner !!}
|
| 145 |
warnifnot <- stopifnot ; N <- length(bdy <- body(warnifnot))
|
141 |
warnifnot <- stopifnot ; N <- length(bdy <- body(warnifnot))
|
| 146 |
bdy <- do.call(substitute, list(bdy, list(stopifnot = quote(warnifnot))))
|
142 |
bdy <- do.call(substitute, list(bdy, list(stopifnot = quote(warnifnot))))
|
| 147 |
bdy[[N-1]] <- do.call(substitute, list(bdy[[N-1]], list(stop = quote(warning))))
|
143 |
bdy[[N-1]] <- do.call(substitute, list(bdy[[N-1]], list(stop = quote(warning))))
|