I am trying to use a slot of type formula. But formula isn't a basic datatype. What can I do to create a slot to store objects like formula. Or is it intentional to prohibit storing general S3 objects as slots? If it's intentional to use slots of type S4, how do I turn the S3 class formula into S4 class?
Asked
Active
Viewed 213 times
3 Answers
2
Seems to work for me:
setClass("form", representation(f="formula"))
myForm <- new("form",f=y~x)
myForm
An object of class "form"
Slot "f":
y ~ x
class(myForm@f)
[1] "formula"

James
- 65,548
- 14
- 155
- 193
-
Thax, this structure was also my first trail, and I got an error "cannot use object of class “formula” in new(): class “Myclass” does not extend that class". But your code works fine. But in general, is there a way to use other S4 classes as slot types? – Klaus Jun 29 '12 at 12:17
1
Maybe it helps if you use named arguments in your call to new
. S4 classes as slots are supported.
setClass(Class = "B", representation = representation(var1 = "character"))
setClass(Class = "A", representation = representation(var1 = "B"))
b<-new("B",var1="b")
a<-new("A",var1=b)

Karsten W.
- 17,826
- 11
- 69
- 103
0
I would like to use a mechanism like
class B{
int varb=0;
}
class A{
B classvar;
A(B var) classvar=var;
}
b=new B();
a=new A(b);
Currently I do it in this way
setClass(Class = "A",
representation = representation(var1 = "list")
)
setClass(Class = "B",
representation = representation(var1 = "character")
)
b<-new("B","b")
a<-new("A",list(b))
to save S4 object like a class variable.

Klaus
- 1,946
- 3
- 19
- 34