[R] Bug report: override stopifnot() ?

Stepan Kasal kasal at ucw.cz
Mon Mar 12 18:53:00 CET 2018


On Mon, Mar 12, 2018 at 09:30:59AM -0700, William Dunlap wrote:
> Why don't you use
>    stopifnot( all(m1 == m2) )
> ?

good question.  Even though I use
   aseert np.all(m1 == m2)
when working with NumPy, I got accustomed to the "handy shortcut"
that I can omit all() with R vectors and matrices.

Then I got trapped with the thing I reported.

On a second thought, omitting all() might have been bad idea from
the beginning; I should rather write all() routinely.
(It also reminds me that all.equal() is the right one in most cases.)

Is it true that using stopifnot() with non-scalar is considerd bad style?

If yes, could be perhaps stopifnot() enhanced to issue a warning to
teach new users of R, at least when they start using library(Matrix)?

If not, then enhancing stopifnot() to handle the case may be a good idea.

I also noticed the following:

> a <- Matrix(1)
> stopifnot(a == a)
Error: a == a is not TRUE
> if(a==a)print(1)
Error in if (a == a) print(1) : argument is not interpretable as logical

Neither does work, but the first error message is much more confusing.

When thinking about it, stopifnot() should really issue a better error
message in this case.  Patch attached.  But I should perhaps send
it also to R-devel.

Stepan Kasal

> On Mon, Mar 12, 2018 at 8:15 AM, Stepan Kasal <kasal at ucw.cz> wrote:
> > Hello,
> > I stumbled over a problem:
> >    stopifnot(m1 == m2)
> >
> > It works with vector or matrix, but does not work for classes from Matrix
> > package.
> >
> > In the source of stopifnot(), there is all(m1 == m2) that would just work,
> > but there is also is.logical(m1 == m2) that id FALSE.
> >
> > Would it be possible if Matrix package redefined stopifnot() ?
> >
> > (If there is a bug tracking database for package Matrix, I would be happy
> > to insert this report there.)
> >
> > Thank you very much for the package,
> >     Stepan Kasal
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/
> > posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >

More information about the R-help mailing list