[Rd] stopifnot

Suharto Anggono Suharto Anggono @uh@rto_@nggono @end|ng |rom y@hoo@com
Sun Feb 24 15:22:48 CET 2019


>From https://github.com/HenrikBengtsson/Wishlist-for-R/issues/70 :
... and follow up note from 2018-03-15: Ouch... in R-devel, stopifnot() has become yet 4-5 times slower;

...
which is due to a complete rewrite using tryCatch() and withCallingHandlers().


>From https://stat.ethz.ch/pipermail/r-devel/2017-May/074256.html , it seems that 'tryCatch' was used to avoid the following example from giving error message with 'eval' call and 'withCallingHandlers' was meant to handle similar case for warning.
tst <- function(y) { stopifnot(is.numeric(y)); y+ 1 }
try(tst())

However,
withCallingHandlers(<something>,
warning = function(w) { w$call <- cl.i; w })
actally has no effect. In current code of function 'stopifnot', 'eval' is used only in handling stopifnot(exprs=) . The warning message from
stopifnot(exprs={warning()})
has 'eval' call:
In eval(cl.i, envir = envir) : 

This may work.
withCallingHandlers(<something>,
warning = function(w) {
w$call <- cl.i; warning(w); invokeRestart("muffleWarning") })


Current documentation says:
Since R version 3.5.0, expressions are evaluated sequentially, and hence evaluation stops as soon as there is a "non-TRUE", asnindicated by the above conceptual equivalence statement. Further, when such an expression signals an error or warning, its conditionCall() no longer contains the full stopifnot call, but just the erroneous expression.

I assume that "no longer contains ..." is supposed to be the effect of the use of 'withCallingHandlers' and 'tryCatch' in 'stopifnot'.

Actually, "contains the full stopifnot call" is not always the case in R before version 3.5.0. Normally, the call is the "innermost context".

Example:
stopifnot((1:2) + (1:3) > 0)
Warning message:
In (1:2) + (1:3) :
  longer object length is not a multiple of shorter object length

Example that gives error:
stopifnot(is.na(log("a")))
R 3.5.0:
R 3.3.2:



More information about the R-devel mailing list