参考类字段消失

时间:2012-08-05 11:26:58

标签: r cran reference-class

我决定给参考类另一个镜头,但我的第一个问候世界已经给了我一些问题。这里出了什么问题?

> 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)

1 个答案:

答案 0 :(得分:5)

我对Reference Classes不太熟悉,但根据帮助页面(?ReferenceClasses),我认为您必须在课程中添加show方法才能自动打印你的对象。

memory <- setRefClass(
          Class = "memory",
          fields = list(state="vector"),
          methods = list(
          get = function() { return(state) },
          set = function(x) { state <<- x },
          show = function() {methods::show(state)}
          )
          )$new()


memory$set(123)
print(memory)
#[1] 123

memory$get()
#[1] 123


print(memory)
#[1] 123

希望这个帮助