1

Is there any way to use a private value in order to create another private value inside R6Class()? I am getting errors.

obj <- R6Class(
  "abc",
  private = list(
    a = 2,
    b = 2*private$a
  )
)

Error in all_named(private) : object 'private' not found

I have also tried to create b with b = 2 * a, but it is still impossible. How should I go about it?

Thank you

SavedByJESUS
  • 3,262
  • 4
  • 32
  • 47

1 Answers1

1
obj <- R6Class(
   "abc",
  private = list(
    a = 2,
    b = function() 2*self$a
   )
)
MaHo
  • 316
  • 1
  • 4
  • 11
  • I just tried it and it does not work unfortunately. Instantiating `obj` and calling the method `b` produces `NULL` instead of `4`. Also, this is a suggestion and should not be posted as an answer. Thank you. – SavedByJESUS Oct 24 '17 at 13:36
  • What else than NULL it should return? It's private. I've edited my answer so that it is not a suggestion any more :-) – MaHo Oct 24 '17 at 13:49
  • I guess I will edit my question. I was under the impression that calling `b` from a public method would return `4`? – SavedByJESUS Oct 24 '17 at 13:58
  • 1
    Alright then, I'll leave my answer nevertheless. I'd recommend reading https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html which states the essentials about OOP in R. – MaHo Oct 24 '17 at 14:01