[R] Extending a group of S4 classes by setClassUnion ?

Martin Morgan mtmorgan at fhcrc.org
Thu Mar 15 19:00:23 CET 2012


On 03/15/2012 09:51 AM, Alexander wrote:
> Hi Martin,
> thanks for your quick answer. I didn't know that '.' could be missleading.
> Is there any standard way to name function for S4 objects? "get","set"
> etc..?

Hi Alexander -- it's usually better to include the original email in the 
reply, to provide context especially for those joining the thread later.

I think 'get' and 'set' are implicit in the action of the 'getter' 
fun(x) and 'setter' fun(x) <- value function. I would have written (I 
wouldn't have used par, which is an existing function unrelated to what 
you're trying to do).

   setGeneric("parent",
       function(object, ...) standardGeneric("parent'))
   setMethod("parent", "Father", function(object, ...) object at name)

   setGeneric("parent<-",
       function(object, ..., value) standardGeneric("parent<-"))
   setReplaceMethod("parent", c("Father", "Son1"),
       function(object, ..., value)  {
           object at name <- value
           object
       })

and used as

   parent(obj)
   parent(obj) <- son

I realize I'm confused about Father / Son and 'parent' here, maybe you 
meant something else by 'par'.


> I saw your example, and I was wondering, why get.par(ext) put out "Son1",
> and not the same as get.par(new("Son1", name="Son1", par=3))

the setIs established a relationship between Extension and Father; you 
could have established a relationship between Extension and Son1

   setIs("Extension", "Son1", <...>)

and then you would get your expected result. I have to say that I have 
rarely used setIs, so the complexity of inheritance may hold some 
surprises, e.g., when there are setIs defined, from Extension to Father, 
Son1, and Son2.

Martin

> If I define a function just like setMethod("get.par", "Father",
> function(object) object at name) , for example
>
> setMethod("get.par", "Father", function(object) object at par)
>
> then it would work, for all objects, which are either Son1 or Son2, but not
> all object, which are "Father", contains also the variable par
>
>   get.par(new("Father"))
>
> Alexander
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Extending-a-group-of-S4-classes-by-setClassUnion-tp4475251p4475650.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.


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

Location: M1-B861
Telephone: 206 667-2793



More information about the R-help mailing list