Steven Lacey
2006-Jan-19 04:38 UTC
[R] creating objects with a slot of class formula, using new
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 objectfor 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]]
Gabor Grothendieck
2006-Jan-19 05:27 UTC
[R] creating objects with a slot of class formula, using new
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 >
Seth Falcon
2006-Jan-19 07:36 UTC
[R] creating objects with a slot of class formula, using new
On 18 Jan 2006, slacey at umich.edu wrote:> 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?Because 'formula' is not a formal S4 class. new("formula") doesn't work. And because isVirtualClass(getClass("formula")) returns TRUE, which I find surprising since one can have an instance of a formula. One workaround is to define a prototype: setClass("a", representation(b="list", c="formula"), prototype=prototype(c=formula(~1))) + seth