[R] Where to declare S4 classes?

Seth Falcon sfalcon at fhcrc.org
Sun Mar 5 18:22:21 CET 2006

Hi Patrick,

Patrick Giraudoux <patrick.giraudoux at univ-fcomte.fr> writes:
> OK. Looks like I have got it... going through the source code of sp, 
> classes are declared as usual functions in *.R files. 

I'll add in a few suggestions that I have found useful for developing
heavily S4 classes (you don't have to do it this way, your life may
not be better if you do, etc):

File organization:

* Put all setClass calls in AllClasses.R
* Put all setGeneric calls in AllGenerics.R
* Put setMethod("foo", ...) calls in foo-methods.R


* use a NAMESPACE file

Why do I find this helpful?

With all of the classes in one place it is easy to see what *things*
the package defines, how they are structured, and how they relate to
each other.  Similarly, having all the generics in one place provides
a nice overview of the behaviors defined by the package.

Methods separate from the classes makes sense because in S4 methods
don't belong to classes.  Since classes and methods are decoupled in
S4, it makes sense to me to organize them that way in the source code.


+ seth

More information about the R-help mailing list