[R] creating objects with a slot of class formula, using new

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 19 06:27:42 CET 2006


Create a virtual class that can either be a formula or be NULL:

   setClassUnion("formulaOrNULL", c("formula", "NULL"))
   setClass("a",representation(b="list",c="formulaOrNULL"))
   new("a", b = list(7))

On 1/18/06, Steven Lacey <slacey at umich.edu> wrote:
> Hi,
>
> This works fine.
> >setClass("a",representation(b="list",c="list"))
> >new("a",b=list(7))
> >An object of class "a"
> Slot "b":
> [[1]]
> [1] 7
>
>
> Slot "c":
> list()
>
> But, now suppose you want a slot to accept an object of class formula...
> >setClass("a",representation(b="list",c="formula"))
> >new("a",b=list(7))
> >Error in validObject(.Object) : invalid class "a" object: invalid object
> for slot "c" in class "a": got class "NULL", should be or extend class
> "formula"
>
> Why can't new handle this? Why must the slot be defined?
>
> If I call new without any named arguments, it works fine
> > new("a")
> An object of class "a"
> Slot "b":
> list()
>
> Slot "c":
> NULL
>
> If I call new with only a formula, it works fine.
> > new("a",c=formula(x~y))
> An object of class "a"
> Slot "b":
> list()
>
> Slot "c":
> x ~ y
>
> How can I get R to do this?
> >setClass("a",representation(b="list",c="formula"))
> >new("a",b=list(7))
> An object of class "a"
> Slot "b":
> [[1]]
> [1] 7
>
> Slot "c":
> NULL
>
> Thanks,
> Steve
>
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    1.1
> year     2005
> month    06
> day      20
> language R
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list