[Rd] Troubles using numeric in s4 class union

Ezra Tucker ezztucker @end|ng |rom gm@||@com
Mon Nov 11 22:47:41 CET 2019


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?
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?
4. Is this a bug in Base or Methods?

Thank you for your time!

Sincerely,
Ezra

	[[alternative HTML version deleted]]



More information about the R-devel mailing list