Skip to content

class unions?

2 messages · Parlamis Franklin, John Chambers

#
the code below has me confused:

	setClassUnion("index", c("numeric", "character", "logical"))
	extends("numeric") # i don't see the class union
	library(Matrix)
	extends("numeric") # now i see the class union

i am aware that the "Matrix" package separately defines the "index"  
class union.
is it the case that class definitions need to occur in packages?

i am also having trouble dispatching on class unions defined in the  
global environment,
which may just be the same problem. see code below:

	setClassUnion("numORchar", c("numeric", "character"))
	foo <- function(x) "Default"
	setMethod("foo", signature(x = "numORchar"),
		function(x) "numORchar")
	foo(2) # gives the default method

franklin

 > version
                _
platform       powerpc-apple-darwin8.7.0
arch           powerpc
os             darwin8.7.0
system         powerpc, darwin8.7.0
status
major          2
minor          4.0
year           2006
month          10
day            03
svn rev        39566
language       R
version.string R version 2.4.0 (2006-10-03)
#
The problem is a combination of a class union definition that includes a 
basic type as a member, and is not in a package.

Class "numeric" is sealed, so at present setClassUnion() doesn't cache 
the new superclass in the definition of "numeric".  In 2.4.0 that 
happens when a package is attached (before, it didn't happen at all).  
It's _probably_ true that the caching should take place in 
setClassUnion(), but that needs a little consideration.

Meanwhile the workaround is to call cacheMetaData(1) to cache the 
information in the global environment--that's what happens when a 
corresponding package is attached.

 > setClassUnion("index", c("numeric", "character", "logical"))
[1] "index"
 > cacheMetaData(1)
 > extends("numeric")
[1] "numeric"   "vector"    "index"
Parlamis Franklin wrote:
Yes.  You need to cacheMetaData(1) before calling foo(2).