Re: [R] S4 slot containing either aov or NULL

From: Matthias Kohl <Matthias.Kohl_at_stamats.de>
Date: Wed, 26 Nov 2008 19:17:05 +0100

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

______________________________________________
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.
Received on Wed 26 Nov 2008 - 18:19:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 01 Dec 2008 - 15:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive