From m@ech|er @end|ng |rom @t@t@m@th@ethz@ch  Wed Nov 27 12:52:48 2019
From: m@ech|er @end|ng |rom @t@t@m@th@ethz@ch (Martin Maechler)
Date: Wed, 27 Nov 2019 12:52:48 +0100
Subject: [Rd] Troubles using numeric in s4 class union
In-Reply-To: <CAP0QTepYgET5x+5aAZGwXKCZ+fs8bhHH5aswL1s3oebv6feU9A@mail.gmail.com>
References: <CAP0QTepYgET5x+5aAZGwXKCZ+fs8bhHH5aswL1s3oebv6feU9A@mail.gmail.com>
Message-ID: <24030.25488.546854.295498@stat.math.ethz.ch>

>>>>> Ezra Tucker 
>>>>>     on Mon, 11 Nov 2019 21:47:41 +0000 writes:

> Hi all,
> 
> I came across an issue in using the Matrix package which made it that I
> could only subset Matrices using the numeric class, but could not using
> integers. Steps to reproduce the problem:
> 
> library(Matrix)
> 
> # this class has *nothing* to do with Matrix
> setClass("MyClass",
>     representation(myvalue = "numeric"),
>     prototype(myvalue = NA_real_))
> # this class also has nothing to do with Matrix
> setClassUnion("NumOrMyClass", c("numeric", "MyClass"))
> 
> # this is to demonstrate creating and subsetting a specific Matrix
> m <- new("dgCMatrix", i = c(2L, 0L, 1L, 2L, 0L, 1L), p = c(0L, 1L, 2L, 4L,
> 4L, 6L),
>     Dim = c(3L, 5L), Dimnames = list(NULL, NULL), x = c(2, 1, 2, 1, 2, 1),
> factors = list())
> # this next line fails:
> m[1:2, 1:2]
> 
> The last line fails consistently with R 3.6.1 and Matrix 1.2-17 on a
> variety of operating systems and computers (tested with ubuntu 19.10 &
> windows 10). I'll note at this point that without the class union I defined
> above, the rest of the code works perfectly fine.
> 
> There are 2 workarounds I've come up with. Firstly, one could change the
> last line to
> m[as.numeric(1:2), as.numeric(1:2)]
> or as.numeric(seq()) or c(1, 2), etc.
> 
> The other workaround is to change setClassUnion("NumOrMyClass",
> c("numeric", "MyClass")) to setClassUnion("NumOrMyClass", c("numeric",
> "double", "integer", "MyClass")), which I believe should be the recommended
> workaround here.
> 
> The underlying issue, though, is that I would not have expected *anything*
> that I write to get in the way of Matrix; in fact, the classes that I
> defined have nothing to do with Matrix at all. So, I have some fears that
> this might be a bigger problem, possibly either in Methods or in Base.
> 
> 1. Can others confirm that this is in fact an issue as I believe it to be?

yes, others have confirmed this is an issue .. (unfortunately
not in this R-devel thread).

Thank you, Ezra, very much for your helpful report, including a
simple reproducible example.

> 2. If it is a legitimate issue, have others seen it manifested working with
> other packages that are not Matrix? In this case, I believe that the class
> union I defied is somehow interfering with the "index" class union in
> Matrix, R/AllClass.R, line 809

> 3. Why should the class union I defined interfere with the inner workings
> of a separate package?

There is no good reason ...

> 4. Is this a bug in Base or Methods?

This is a bug in "base R", in package 'methods'.

The R core team had taken the issue up, already two weeks ago,
but unfortunately did not get to address this in a definitive
way.  ==>  I'll remind us about it !

Martin Maechler
ETH Zurich and R Core


> Thank you for your time!
> 
> Sincerely,
> Ezra



[Rd] Troubles using numeric in s4 class union
Martin Maechler