[R] creating objects with a slot of class formula, using new
Seth Falcon
sfalcon at fhcrc.org
Thu Jan 19 08:36:43 CET 2006
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
More information about the R-help
mailing list