[R] locked environment and inheritance

Seth Falcon sfalcon at fhcrc.org
Tue Jun 5 19:04:05 CEST 2007


johan Faux <johanfaux at yahoo.com> writes:

> Thanks for your reply.
>
> Experimenting a little further, I was able to learn a bit more and
> find what is the real problem of mine what the real question is.

>
> I could create my S4 extended class by adding these two lines in my namespace file
> importClassesFrom(Matrix,Matrix)
> importClassesFrom(Matrix,dMatrix)

You can also do:

  importClassesFrom(Matrix, Matrix, dMatrix)

> and then creating my new class in another R file
>
> library(Matrix)
  ^^^^^^^

I would not call library for such a case.  If you are importing
library in your package's namespace, then you probably want the
following in your DESCRIPTION file:

   Depends: Matrix, methods
   Imports: Matrix

[yes, there is a lot of duplication in how things are specified]

> setClass("myMatClass", representation("dMatrix",myslot="numeric"))
>
>
> which creates the new S4 class when my package is loaded. 
>
> The problem is that I would like to create the class inside a function of mine:
>
> createMyS4Class() {
>     library(Matrix)
>
>     setClass("myMatClass", representation("dMatrix",myslot="numeric"))
> }

Again, the call to library should not be needed.

> so that, at some point (after my package is loaded ), I can do something like:
>
> if (cond)
>     createMyS4Class()

How do you intend to use such a class?  Is the real issue that you
don't want to depend on Matrix and only use it if it is available?

> Now, I understand that at this point, my package environment is
> locked.  So the question become: Is there any way that I can unlock
> my package environment, create my new S4 class and lock it back or
> something?

I think that might be possible, but that doesn't make it a good idea :-)

> Please let me know if that makes sense ? 
> I appreciate your comments.

It might help to understand what your are trying to achieve overall.
Perhaps there are other approaches...

Also, at this point, the discussion is more appropriate for R-devel.
Perhaps you can move the conversation there?

Best,

+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org



More information about the R-help mailing list