I'm trying to forget refclasses (R5) and move to R6 but there is a problem with dynamic code. I would add a new function and it works in R5:
clsTrn <- setRefClass("clsTrn",
fields = list(x = "numeric"),
methods = list(
add_function = function(rcode) {
eval(parse(text=rcode), envir=.self)
}
)
)
cls <- clsTrn$new(x=4)
cls$x
# [1] 4
cls$add_function("predict = function(y) {return(.self$x*y)}")
cls$predict(3)
#[1] 12
Similar code doesn't work for R6.
library(R6)
clsTrnR6 <- R6Class("clsTrnR6",
lock=FALSE,
public = list(
x = NA,
initialize = function(x) {
self$x <- x
},
add_function = function(rcode) {
eval(parse(text=rcode), envir=self)
}
)
)
clsR6 <- clsTrnR6$new(x=4)
clsR6$x
#[1] 4
clsR6$add_function("predict = function(y) {return(self$x*y)}")
# B??d weval(expr, envir, enclos) : nie uda?o si? znale?? funkcji '='
clsR6$predict(3)
Adding predict in class definition changes nothing, the same error.
Is there any solution? Thanks in advance.
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=pl_PL.UTF-8 LC_NUMERIC=C LC_TIME=pl_PL.UTF-8 LC_COLLATE=pl_PL.UTF-8 LC_MONETARY=pl_PL.UTF-8
[6] LC_MESSAGES=pl_PL.UTF-8 LC_PAPER=pl_PL.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=pl_PL.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] R6_2.0
loaded via a namespace (and not attached):
[1] codetools_0.2-8 rpart_4.1-5 tools_3.1.1
>
Added: After great @G.Grothendieck answer, I have string based function definition, but maybe there is more elegant solution.
library(R6)
clsTrnR6 <- R6Class("clsTrnR6",
lock=FALSE,
public = list(
x = NA,
initialize = function(x) {
self$x <- x
},
add_function = function(name, meth) {
self[[name]] <- meth
environment(self[[name]]) <- environment(self$add_function)
},
add_function2 = function(name, meth) {
eval(parse(text=paste0("predict <- ",meth)))
self[[name]] <- predict
environment(self[[name]]) <- environment(self$add_function)
}
)
)
clsR6 <- clsTrnR6$new(x=4)
clsR6$x
#[1] 4
clsR6$add_function2("predict", "function(y) y*self$x")
clsR6$predict(11)
#[1] 44
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…