[R] learning S4

Martin Morgan mtmorgan at fhcrc.org
Sat Feb 9 02:29:23 CET 2008


Hi Christophe --

cgenolin at u-paris10.fr writes:

> Hi the list.
>
> I try to learn the S4 programming. I find the wiki and several doc. But 
> I still have few questions...
>
> 1. To define 'representation', we can use two syntax :
>     - representation=list(temps = 'numeric',traj = 'matrix')
>     - representation(temps = 'numeric',traj = 'matrix')
>    Is there any difference ?

See the help page for representation

> ?representation

for a description of the checks performed by this function.  View the
function definition for 'representation' with

> representation

to see the implementation details.

> 2. 'validityMethod' check the intialisation of a new object, but not 
> the latter
>    modifications. Is it possible to set up a validation that check every
>    modifications ?

No. A solution is to create 'setters' that use slot<- or @<- and then
check validity explicitly; the 'user' would then invoke the setters
and avoid direct slot access.. 

> 3. When we use setMethod('initialize',...) does the validityMethod 
> become un-used ?

'initialize' for signature .Object="ANY" calls validObject sometimes
(when length(list(...))>0; see

> getMethod("initialize", "ANY")

for implementation details). So if your 'initialize' uses
callNextMethod (I think object oriented practice would encourage this)
and the appropriate conditions apply, then validity gets called. Some
'initialize' paradigms might not work very well with this setup, e.g.,

   .Object <- callNextMethod() # validObject
   # further modify .Object and hence check validity again??

> 4. Is it possible to set up several initialization processes ?   One 
> that build an objet from a data.frame, one from a matrix...

No, 'initialize' has only a single argument to dispatch on (.Object,
based on the prototype of the object being created) and so cannot be
specialized for additional arguments. Solutions include writing
conditional code within your 'initialize', or to create a new
'constructor' generic with it's own signature that performs initial
coercion before calling 'new'. Personally I think this is a good
solution, treating 'new' and 'initialize' as internal functions that
the 'user' does not invoke directly.

Hope that helps,

Martin

> Thanks
>
> Christophe
>
> ----------------------------------------------------------------
> Ce message a ete envoye par IMP, grace a l'Universite Paris 10 Nanterre
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-help mailing list