为了构建通用框架,我想构建一个s4类,其插槽可以是任何类型。可能吗?如果是这样怎么办?
我发现这个问题解决了多类型问题。但是我想让任何类型的对象都可以,而不仅仅是一些预定义的类型。
setClass("foo",
representation(
anything = "..."
)
)
# I would like to be able to perform all of these
new("foo", anything = 1)
new("foo", anything = "a")
new("foo", anything = data.frame())
...
Run Code Online (Sandbox Code Playgroud)
是的,您可以执行以下操作:
setClass("hi", slots = c(slot1 = "ANY"))
Run Code Online (Sandbox Code Playgroud)
采用ANY在帮助实际记录。