[R] SOLVED: passing reference class method within the class as an argument

Eric Berger er|cjberger @end|ng |rom gm@||@com
Fri Dec 6 08:27:03 CET 2019


Here is the modified code of the reproducible example I sent previously.
Notice the .self$ prefix to funcB which is what has changed

foo <- setRefClass("foo",
                   fields=list(x="numeric"),
                   methods=list(
                       initialize=function(a) {
                           x <<- a
                       },
                       funcA=function(f) {
                           f(x)+f(x)
                       },
                       funcB=function(y) {
                           y^2
                       },
                       funcC=function() {
                           u <- funcA(.self$funcB)
                           u
                       }
                   ))

w <- foo(2)
z <- w$funcC()
print(z)

# 8

	[[alternative HTML version deleted]]



More information about the R-help mailing list