Pregunta
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?
Solución
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"
Otros consejos
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)
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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow