[R] S4 slot containing either aov or NULL

Thomas Kaliwe hamstersquats at web.de
Mon Dec 1 15:54:32 CET 2008


Thank you!

Matthias Kohl schrieb:
> Dear Thomas,
>
> take a look at setOldClass ...
>
> ## register "aov" (an S3-class) as a formally defined class
> setOldClass("aov")
>
> ## "list" and some others are special cases; cf.
> class ? list
>
> ## now your code should work
> setClassUnion("aovOrNULL", c("aov", "NULL"))
> setClass("c1", representation(value = "aovOrNULL"))
> y1 <- new("c1", value = NULL)
>
> #trying to assign an aov object to the slot doesn't work
> utils::data(npk, package="MASS")
> npk.aov <- aov(yield ~ block + N*P*K, npk)
> y2 <- new("c1", value = npk.aov )
>
> Best,
> Matthias
>
> Thomas Kaliwe wrote:
>> Dear listmembers,
>>
>> I would like to define a class with a slot that takes either an 
>> object of class aov or NULL. I have been reading "S4 Classes in 15 
>> pages more or less" and "Lecture: S4 classes and methods"
>>
>> #First I tried with list and NULL
>> setClass(listOrNULL")
>> setIs("list", "listOrNULL")
>> setIs("NULL", "listOrNULL")
>> #doesn't work
>>
>> #defining a union class it works with list and null
>> setClassUnion("listOrNULL", c("list", "NULL"))
>> setClass("c1", representation(value = "listOrNULL"))
>> y1 = new("c1", value = NULL)
>> y2 = new("c1", value = list(a = 10))
>>
>> #but it won't work with aov or null
>> setClassUnion("aovOrNULL", c("aov", "NULL"))
>> setClass("c1", representation(value = "aovOrNULL"))
>> y1 = new("c1", value = NULL)
>>
>> #trying to assign an aov object to the slot doesn't work
>> utils::data(npk, package="MASS")
>> npk.aov <- aov(yield ~ block + N*P*K, npk)
>> y2 = new("c1", value = npk.aov )
>>
>> Any ideas?
>>
>> Thank you
>>
>> Thomas Kaliwe
>>
>> ______________________________________________
>> 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.
>



More information about the R-help mailing list