Question

Is it possible to declare a variable btw. representation like "obj" of a arbitrary type, that means an ancestor, where all data types derived from? So that I can appropriate each type to representation type? Should I put this flexible parameter into a list?

setClass(Class = "MyClass",
         representation = representation(obj = "object"
                                         )
         )


new("MyClassA",name="abc",typ=123)
Was it helpful?

Solution

Yes, this is possible by setting the class of a slot to be ANY. For instance:

setClass('foo', slots = c(obj = 'ANY'))

Then the slot obj in the class foo can be used to store an object of any class.

Finally, the representation parameter to setClass was recently deprecated with version 3.0.0 of R, and you should use the slots parameter (as above) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top