[R] Bug report: override stopifnot() ?

Duncan Murdoch murdoch.duncan at gmail.com
Mon Mar 12 21:45:19 CET 2018


On 12/03/2018 2:43 PM, Bert Gunter wrote:
> Please stop this line of queries/"suggestions/speculations and read the
> relevant docs **carefully**.
> 
> For example, from ?"=="
> 
> "Note
> 
> Do not use == and != for tests, such as in if expressions, where you must
> get a single TRUE or FALSE. Unless you are absolutely sure that nothing
> unusual can happen, you should use the identical
> <http://127.0.0.1:22171/help/library/base/help/identical> function instead.
> "

But stopifnot(expr) is not a test where you must get a single TRUE or 
FALSE.  See the Details section on its help page, or read its 
Description carefully (where "all" is used in the technical sense of the 
all() function), ignoring the final few words which seem to suggest that 
c(TRUE, TRUE) is not okay.

Duncan Murdoch

> 
> So you have already violated that specific warning, which led to the
> confusion you evidence. Specifically:
>> Matrix(1)== Matrix(1)
> 1 x 1 Matrix of class "lsyMatrix"
>       [,1]
> [1,] TRUE
> 
> That is, the result is not a logical but a (S4) object of class "lsyMatrix"
> that contains a logical. Whence your (expected) error message.
> 
> Cheers,
> 
> Bert
> 
> 
> 
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Mon, Mar 12, 2018 at 10:53 AM, Stepan Kasal <kasal at ucw.cz> wrote:
> 
>> Hello,
>>
>> 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.
>>>>
>>
>> ______________________________________________
>> 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.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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