1

我想在 R6Class 上编写包装器,但它不起作用,在我发现这个问题后,我尝试了多种方法,将函数动态添加到 r6 类实例

所以我试过这个,它们都不起作用:

get <- function(x = list()) {
  class <- R6::R6Class(classname = "class")
  for (name in names(x)) {
    class$set("public", name, function() name)
  }
  class
}

x <- get(x = list(a = 10, b = 20))$new()
x$a()
# b
x$b()
# b

这是因为带有闭包的循环 for 不会创建新范围。所以我试过这个:

get <- function(x = list()) {
  class <- R6::R6Class(classname = "class")
  lapply(names(x), function(name) {
    print(name)
    class$set("public", name, function() name)
  })
  class
}

x <- get(x = list(a = 10, b = 10))$new()
x$a()

这将引发 name 未定义的错误,因为 R6Class 的这种行为是一切都在eval substitute,因此无法创建从调用它的位置获取范围/环境的新函数。或者有什么办法吗?

我真正的问题是我想创建函数包装器并且我想调用:

fn <- function() {
    x <- 10
    y <- myFunction(public = list(
       foo = function(y) {
          x + y
       }
    })
    z <- y$new()
    z$foo(10)
    ## I want 20 as result
}

有没有办法创建myFunction将创建 R6Class 的函数?我想要这个的原因是因为我有基于 R6Class 的组件系统,并且想要删除一些需要添加到每个类中的样板,以便更容易使用。我不想创建新的班级系统,我想使用 R6 班级。

4

1 回答 1

1

添加此问题后,我在GitHub 上提问,他们很快给出了答案。这是答案的重新发布:

get <- function(x = list()) {
  class <- R6::R6Class(classname = "class")
  lapply(names(x), function(name) {
    fn <- eval(substitute(function() subst_name, list(subst_name = name)))
    class$set("public", name, fn)
  })
  class
}
x <- get(x = list(a = 10, b = 20))$new()
x$a()

如果您在打印 x$a 时想要更好的名称,您可以使用以下命令清除名称引用:

attr(fn, "srcref") <- NULL

编辑

这是添加到类的值是函数的示例(这是我改进的代码):

constructor <- function(public = list(), private = list()) {
  class <- R6::R6Class(classname = "class")
  lapply(names(public), function(name) {
    if (is.function(public[[name]])) {
      env <- environment(public[[name]])
      env$self <- public
      env$private <- private
      fn <- eval(substitute(function(...) fn(...), list(fn = public[[name]])))
      class$set("public", name, fn)
    } else {
      class$set("public", name, public[[name]])
    }
  })
  class
}
test <- function() {
  a <- 10
  class <- constructor(
     public = list(
         a = function() { a + self$b },
         b = 20
     )
  )
  x <- class$new()
  x$a()
}

test()

如果您想访问 super 您需要使用以下代码:

编辑2

component <- function(public = NULL,
                      private = NULL,
                      static = NULL,
                      ...) {
  class <- R6::R6Class(...)
  r6.class.add(class, public)
  r6.class.add(class, private)
  class$extend <- make.extend(class)
  class
}

#' helper function for adding properties to R6Class
r6.class.add <- function(class, seq) {
  prop.name <- as.character(substitute(seq)) # so we don't need to write name as string
  lapply(names(seq), function(name) {
    if (is.function(seq[[name]])) {
      ## the only way to have scope from when function was create with self and private
      ## eval substitute simply circument R6 encapsulation and use scope from where function
      ## was created (closure) and env.fn patch the env of inner function so it get self
      ## and private as magic names - this is done so component function the same as
      ## when R6Class is created inline - so component is referencial transparent and can
      ## be replaced with R6Class
      fn <- eval(substitute(function(...) {
        ## patch function env
        fn <- fn.expr # fn.expr will be inline function expression
        parent <- parent.env(environment())
        ## we don't overwrite function scope so you can nest one constructor
        ## in another constructor
        env <- new.env(parent = environment(fn))
        env$self <- parent$self
        env$super <- parent$super
        env$private <- parent$private
        environment(fn) <- env
        fn(...)
      }, list(fn.expr = seq[[name]], name = name)))
      class$set(prop.name, name, fn)
    } else {
      class$set(prop.name, name, seq[[name]])
    }
  })
}

而不是env$self <- parent$self你也可以使用get("self", parent)(它会在环境链中搜索变量)。

于 2019-05-17T16:59:50.613 回答