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

Alexander juschitz_alexander at yahoo.de
Fri Mar 16 09:59:36 CET 2012


Martin Morgan wrote
> 
> 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).
> 
In fact, I have several getter and setter. For example gettermean and
gettervariance are less clear then getter.mean and getter.variance. The
names "mean.getter", "variance.getter" have the disadvantage to have getter
and setter not in first place... (Ok, these are very minor problems...)

Martin Morgan wrote
> 
>    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'.
> 
Yes indeed, "par" means parameter. But you're right, it is to close to
parent and very misleading in this example

Martin Morgan wrote
> 
> 
>> 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@ 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
> 
> ______________________________________________
> R-help@ 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.
> 

I rewrote the example and I think if will have to write some setIs methods.
Do you know, how this is done? Here is a little example which shows, that
the order of initialization for setIs determins the results !!

setClass("Father",representation(name="character"))
setClass("Son1",contains="Father",representation(parameter="numeric"))
setClass("Son2",contains="Father",representation(parameter="logical"))

Son1<-new("Son1",name="Son1",parameter="3")
Son2<-new("Son2",name="Son2",parameter=TRUE)

setGeneric("get.parameter",function(object){standardGeneric
("get.parameter")})
setMethod("get.parameter","Son1",function(object){return(object at parameter+3)})
setMethod("get.parameter","Son2",function(object){return(object at parameter==TRUE)})

get.parameter(Son1)
get.parameter(Son2)

setClass("Extension",representation(person="Father",text="character"))

ext1 <- new("Extension",person=Son1,text="new try")
ext2 <- new("Extension",person=Son2,text="yesyes")

setIs("Extension", "Son1",test=function(from){print("setIsSon1")
		return(class(from at person)=="Son1")
	},
	coerce=function(from) as(from at person,"Son1",strict=FALSE),
	replace=function(from, value) {from at person <- value
		from
	}
)

setIs("Extension", "Son2",test=function(from){print("setIsSon2")
		return(class(from at person)=="Son2")
	},
	coerce=function(from) as(from at person,"Son2",strict=FALSE),
	replace=function(from, value) {from at person <- value
		from
	}
)

get.parameter(ext1) #"setIsSon1", 6 !!!! correct result
get.parameter(ext2)# "setIsSon1", Error in as(object, "Son1", strict =
FALSE) : no method or default for coercing "Extension" to "Son1"

If I open a new R Console and I define setIs("Extension", "Son2", ... )
before setIs("Extension", "Son1", ... ) then I get

get.parameter(ext1) # "setIsSon2", Error in as(object, "Son2", strict =
FALSE) : no method or default for coercing "Extension" to "Son2"
get.parameter(ext2)#"setIsSon2", TRUE !!! correct result

Alexander




--
View this message in context: http://r.789695.n4.nabble.com/Extending-a-group-of-S4-classes-by-setClassUnion-tp4475251p4477649.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list