[Rd] stopifnot() does not stop at first non-TRUE argument

luke-tierney at uiowa.edu luke-tierney at uiowa.edu
Mon May 15 19:41:13 CEST 2017


This is getting pretty convoluted.

The current behavior is consistent with the description at the top of
the help page -- it does not promise to stop evaluation once the first
non-TRUE is found.  That seems OK to me -- if you want sequencing you
can use

stopifnot(A)
stopifnot(B)

or

stopifnot(A && B)

I could see an argument for a change that in the multiple argumetn
case reports _all_ that fail; that would seem more useful to me than
twisting the code into knots.

Best,

luke

On Mon, 15 May 2017, Martin Maechler wrote:

>>>>>> Serguei Sokol <sokol at insa-toulouse.fr>
>>>>>>     on Mon, 15 May 2017 16:32:20 +0200 writes:
>
>    > Le 15/05/2017 à 15:37, Martin Maechler a écrit :
>    >>>>>>> Serguei Sokol <sokol at insa-toulouse.fr>
>    >>>>>>> on Mon, 15 May 2017 13:14:34 +0200 writes:
>    >> > I see in the archives that the attachment cannot pass.
>    >> > So, here is the code:
>    >>
>    >> [....... MM: I needed to reformat etc to match closely to
>    >> the current source code which is in
>    >> https://svn.r-project.org/R/trunk/src/library/base/R/stop.R
>    >> or its corresponding github mirror
>    >> https://github.com/wch/r-source/blob/trunk/src/library/base/R/stop.R
>    >> ]
>    >>
>    >> > Best,
>    >> > Serguei.
>    >>
>    >> Yes, something like that seems even simpler than Peter's
>    >> suggestion...
>    >>
>    >> It currently breaks 'make check' in the R sources,
>    >> specifically in tests/reg-tests-2.R (lines 6574 ff),
>    >> the new code now gives
>    >>
>    >> > ## error messages from (C-level) evalList
>    >> > tst <- function(y) { stopifnot(is.numeric(y)); y+ 1 }
>    >> > try(tst())
>    >> Error in eval(cl.i, pfr) : argument "y" is missing, with no default
>    >>
>    >> whereas previously it gave
>    >>
>    >> Error in stopifnot(is.numeric(y)) :
>    >> argument "y" is missing, with no default
>    >>
>    >>
>    >> But I think that change (of call stack in such an error case) is
>    >> unavoidable and not a big problem.
>
>    > It can be avoided but at price of customizing error() and warning() calls with something like:
>    > wrn <- function(w) {w$call <- cl.i; warning(w)}
>    > err <- function(e) {e$call <- cl.i; stop(e)}
>    > ...
>    > tryCatch(r <- eval(cl.i, pfr), warning=wrn, error=err)
>
>    > Serguei.
>
> Well, a good idea, but the 'warning' case is more complicated
> (and the above incorrect): I do want the warning there, but
> _not_ return the warning, but rather, the result of eval() :
> So this needs even more sophistication, using  withCallingHandlers(.)
> and maybe that really get's too sophisticated and no
> more "readable" to 99.9% of the R users ... ?
>
> I now do append my current version -- in case some may want to
> comment or improve further.
>
> Martin
>
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


More information about the R-devel mailing list