[Bioc-devel] Extend class definition on attaching a package
mtmorgan at fhcrc.org
Fri Aug 27 01:58:08 CEST 2010
On 08/26/2010 12:58 PM, Venkatraman Seshan wrote:
> In AllClasses.R I used
> setClassUnion("cnmatrix", c("matrix"))
> Sorry for the typo.
> On Thu, Aug 26, 2010 at 3:31 PM, Venkatraman Seshan <veseshan at gmail.com>wrote:
>> Can the class definition in package "foo" be changed after the package is
>> attached? I am trying to do the following.
>> The AllClasses.R file has the following class definitions.
>> setClassUnion("mymatrix", c("mymatrix"))
>> setClass("mydata", representation(x="numeric", y="cnmatrix"))
>> In zzz.R the .onLoad has the following:
>> setHook(packageEvent("bigmemory", "attach"),
>> setIs("big.matrix", "cnmatrix")
>> setClass("mydata", representation(x="numeric", y="cnmatrix")
>> When I now do R CMD check pkg it is completed but I get a note
>> * checking R code for possible problems ... NOTE
>> Error in setIs("big.matrix", "cnmatrix") :
>> cannot create a 'setIs' relation when neither of the classes
>> ("big.matrix" and "cnmatrix") is local and modifiable in this package
>> How do I modify the definition of a class in a package?
Hi Venkat -- not really answering your question, but I don't really
think you want to do either setClassUnion or setIs -- inheritance is
difficult enough to understand without adding novel relationships
between classes. And I don't think you really want to conditionally
define class relationships on package load either (which I is guess what
setHook(packageEvent()) accomplishes; I've never seen that before);
these all sound like you're making trouble for yourself (and users of
your package) in the future.
.onLoad is run after Import: and Depend: packages have been loaded, and
before the name space of the package in which .onLoad is defined is
'sealed'. I think this means that if you had Import'ed or Depend'ed on
bigmemory, then a setIs and setClass in .onLoad (not setHook(...)) would
be successful, just as
> setClassUnion("cnmatrix", "matrix")
> setIs("big.matrix", "cnmatrix")
> setClass("mydata", representation(x="numeric", y="cnmatrix"))
are successful when run in an R session.
So I guess what happens is the 'packageEvent' isn't actually evaluated
in .onLoad, but afterward, when bigmemory is loaded (by some other code
in your package? I'm not understanding how R CMD check is detecting the
error) and your name space has already been sealed (which occurs after
the .onLoad hook runs) and is therefore not modifiable. Maybe you could
create an environment in your package
.secrets <- new.env(parent=emptyenv())
and place your class definition there
.secrets isn't sealed, so the class definition might still be
modifiable. I really don't know how this would work, e.g., exporting
cnmatrix from your package name space, invoking methods, etc., or
whether it would provide a way to modify the class definition once the
name space is sealed. My little experiments suggest that you can create
an instance with
and create a setIs relationship, without generating errors or warnings, with
setIs("big.matrix", "cnmatrix", where="secrets")
but all of this seems like a big hack.
>> My R.Version()
>>  "x86_64-pc-linux-gnu"
>>  "x86_64"
>>  "linux-gnu"
>>  "x86_64, linux-gnu"
>>  ""
>>  "2"
>>  "11.1"
>>  "2010"
>>  "05"
>>  "31"
>> $`svn rev`
>>  "52157"
>>  "R"
>>  "R version 2.11.1 (2010-05-31)"
> [[alternative HTML version deleted]]
> Bioc-devel at stat.math.ethz.ch mailing list
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M1 B861
Phone: (206) 667-2793
More information about the Bioc-devel