[R] S4 slot containing either aov or NULL

Matthias Kohl Matthias.Kohl at stamats.de
Wed Nov 26 19:17:05 CET 2008


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.

-- 
Dr. Matthias Kohl
www.stamats.de



More information about the R-help mailing list