Question

I decided to give Reference Classes another shot, but my first hello world is already giving me issues. What is going wrong here?

> 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)
Was it helpful?

Solution

I'm not very experienced with Reference Classes but according to the help page (?ReferenceClasses), I think that you have to add a show method to your class to be able to print automaticaly your object.

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

Hope this help

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