7

我试图忘记引用类(R5)并移至 R6,但动态代码存在问题。我会添加一个新功能,它适用于 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

类似的代码不适用于 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)

在类定义中添加predict没有任何改变,同样的错误。 有什么解决办法吗?提前致谢。

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

补充:在@G.Grothendieck 很好的回答之后,我有基于字符串的函数定义,但也许有更优雅的解决方案。

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
4

3 回答 3

6

Try this. Like the reference class example it adds a function to the object (not the class). Here name is a character string containing the name of the function/method and meth is the function/method itself:

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)
    }
  )
)  
clsR6 <- clsTrnR6$new(x=4)
clsR6$x
#[1] 4
clsR6$add_function("predict", function(y) y*self$x)
clsR6$predict(11)
## 44

Added Note that this is also easy to do using proto. It does not require a special add_function. We will use an upper case P to denote the proto object that plays the role of a class (called a "Trait" in the proto vignette) and use lower case p to denote the proto object that plays the role of an instance:

library(proto)

P <- proto(new = function(., x) proto(x = x))
p <- P$new(x = 4)

p$predict <- function(., y) .$x * y
p$predict(11)
## 44

Although its common to use . to refer to the object in proto you can use the name self (or any name you like) in place of . if you prefer.

于 2014-10-13T02:49:14.590 回答
5

您可以$set() method在生成器对象上使用。因此,您将更改类定义而不是对象。

clsTrnR6$set("public", "predict", function(y) self$x*y)
clsR6 <- clsTrnR6$new(x=4)
clsR6$predict(3)
[1] 12

编辑:

更改类定义意味着在使用 $set 修饰符之前创建的对象将不具有该predict功能。

于 2014-10-13T00:34:25.527 回答
2

这个问题有一个简单的解决方法。将类方法的默认值设置为NULL并在方法内更新此值initialize()。现在,您可以根据需要更改方法而不会收到此错误。例如:

aClass <- R6::R6Class("className",
  public = list(
    f = NULL,
    initialize = function(...) {
      self$f = sum
    },
    update_f = function(x) {
      self$f = x
    }
  )
)

test <- aClass$new()
test$f
test$update_f(mean)
test$f

或者,可以就地修改函数:

test$f <- median
test$f

那应该可以解决问题。我也在这里发布了这个答案。

于 2018-08-06T19:51:46.040 回答