【发布时间】:2012-08-05 11:26:57
【问题描述】:
我决定再试一次 Reference Classes,但我的第一个 hello world 已经给我带来了问题。这里出了什么问题?
> memory <- setRefClass(
+ Class = "memory",
+ fields = list(state="vector"),
+ methods = list(
+ get = function() { return(state) },
+ set = function(x) { state <<- x }
+ )
+ )$new()
> memory$set(123)
> print(memory)
Reference class object of class "memory"
Field "state":
[1] 123
> memory$get()
[1] 123
> print(memory)
Reference class object of class "memory"
Field "state":
Error in methods::show(field(fi)) :
error in evaluating the argument 'object' in selecting a method for function 'show': Error in get(name, envir = .self) :
unused argument(s) (name, envir = .self)
【问题讨论】:
-
很可能与
get是某种保留名称有关;如果我将get重命名为get.state之类的其他名称,您的代码将有效。 -
啊,太好了。不过,
setRefClass并没有为此发出警告,这让我感到不太舒服。
标签: r cran reference-class