Skip to content
Prev 2285 / 21312 Next

[Bioc-devel] Extend class definition on attaching a package

On 08/26/2010 12:58 PM, Venkatraman Seshan wrote:
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.

But...

.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
[1] "cnmatrix"
[1] "mydata"

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())
  setPackageName("MyPackage", .secrets)

and place your class definition there

  setClass("cnmatrix", where=.secrets)

.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

  new(getClassDef("cnmatrix", where=MyPackage:::.secrets))

or by

  attach(MyPackage:::.secrets, name="search")
  new("MyClass")

and create a setIs relationship, without generating errors or warnings, with

  attach(MyPackage:::.secrets, name="secrets")
  setIs("big.matrix", "cnmatrix", where="secrets")
  detach("secrets")

but all of this seems like a big hack.

Martin