R参考类别作为参考类别的字段 [英] R reference classes as a field of a reference class
问题描述
我想将参考类传递给参考类构造函数,并将传递的参考类分配为字段.但是,当我运行下面的代码时,我不明白为什么会收到错误消息.我的问题是:
I would like to pass a reference class to a reference class constructor, and assign the passed reference class as a field. However, when I run the code below, I don't understand why I get an error. My questions are:
1)请有人可以解释一下为什么会发生此错误:
1) Please can someone kindly explain why this error is occurring:
> a <- ClassA&new()
Error in .getClassFromCache(Class, where) :
argument "Class" is missing, with no default
> b <- ClassB$new(a)
Error in .Object$initialize(...) : object 'a' not found
2)我已经将 class.a.container 声明为列表"类,但是我希望这是一个参考类.我需要在这里放置什么而不是列表"?
2) I have declared class.a.container as class "list", however I want this to be a reference class. What do I need to put here instead of "list" ?
ClassA <- setRefClass(
"ClassA",
fields = list(myVar = "numeric"),
methods = list(
someMethod = function(){
print("hi")
}
)
)
ClassB <- setRefClass(
"ClassB",
fields = list(class.a.container = "list"),
methods = list(
initialize = function(class.a){
class.a.container <<- class.a
})
)
a <- ClassA&new()
b <- ClassB$new(a)
推荐答案
您会觉得很傻,至少当我注意到问题时才这样做.从环境类项目中提取的内容中,您有一个&"符号.
Y'er gonna feel kind of silly, at least I did when I noticed the problem. You have an ampersand insead of a dollar-sign in the extraction from the envirnment-class-item.
a <- ClassA$new(myVar=1)
a$someMethod(2)
#[1] "hi
这篇关于R参考类别作为参考类别的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!