[R] A stopifnot() nastiness, even if not a bug
pd@|gd @end|ng |rom gm@||@com
Mon Apr 13 12:00:38 CEST 2020
> On 13 Apr 2020, at 11:15 , Martin Maechler <maechler using stat.math.ethz.ch> wrote:
>>>>>> Bert Gunter
>>>>>> on Sun, 12 Apr 2020 16:30:09 -0700 writes:
>> Don't know if this has come up before, but ...
>>> x <- c(0,0)
>>  2
>> ## but
>> Error: length(x) is not TRUE
>> Called from: top level
>> ## but
>>> stopifnot(length(x) > 0) ## not an error; nor is
>> ## Ouch!
>> Maybe the man page should say something about not assuming automatic
>> coercion to logical, which is the usual expectation. Or fix this.
>> Bert Gunter
> Well, what about the top most paragraph of the help page is not clear here ?
>> If any of the expressions (in '...' or 'exprs') are not 'all'
>> 'TRUE', 'stop' is called, producing an error message indicating
>> the _first_ expression which was not ('all') true.
This, however, is somewhat less clear:
..., exprs: any number of (typically but not necessarily ‘logical’) R
expressions, which should each evaluate to (a logical vector
of all) ‘TRUE’. Use _either_ ‘...’ _or_ ‘exprs’, the latter
What does it mean, "typically but not necessarily ‘logical’"? The code actually tests explicitly with is.logical, as far as I can tell.
This creates a discrepancy between if(!...)stop(...) and stopifnot(), as in
> f <- function (x) if (!x) stop(paste(deparse(substitute(x)), "is not TRUE"))
Error in f(0) : 0 is not TRUE
Error: 0 is not TRUE
Error: 1 is not TRUE
> If useR's expectations alone would guide the behavior of a
> computer language, the language would have to behave
> "personalized" and give different results depending on the user,
> which may be desirable in medicine or psychotherapy but not with R.
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Office: A 4.23
Email: pd.mes using cbs.dk Priv: PDalgd using gmail.com
More information about the R-help