我的目的是重载+
运算符,以便在R6对象上调用方法。这是一个带有R6类对象的玩具示例,该对象aclass
具有一种add_function(foo)
可以自行存储一些任意函数的方法。
我希望将电话替代,以add_function(foo)
通过+ foo
在风格GGPLOT2的+ .gg或+(E1,E2) 。我检查了ggplot2 github代码,但无法弄清楚。
library(R6)
# The class (called aclass) looks like so:
Aclass <- R6Class("aclass",
public = list(
fun = NULL,
x = NULL,
initialize = function(x) {
self$x <- x
return(invisible(self))
},
# this is the function adding part
add_fun = function(fun) {
self$fun <- substitute(fun)
},
# and here we execute the functions
do_fun = function() {
return(eval(self$fun)(self$x))
}))
# Say, we want to add this function to an aclass object
foo <- function(x) mean(x, na.rm=TRUE)
# Instantiate class
my_class <- Aclass$new(c(0,1,NA))
# Add the function
my_class$add_fun(function(x) mean(x, na.rm=TRUE))
# my_class$add_fun(foo) # this also works
# Execute the function - beautiful - returns 0.5
my_class$do_fun()
我的目标是执行与上述相同的操作,即向对象添加foo,但是现在通过重载+运算符以更好的方式完成工作
# Let's try to overload the + operator
`+.aclass` <- function(e1, e2) {
return(e1$add_fun(e2))
}
# option 1
my_class <- Aclass$new(c(0,1,NA)) + foo
# fails
# Looking at my class we get
my_class
# > my_class
# e2
# option 2 <--- this is my end goal
my_class <- Aclass$new(c(0,1,NA)) + mean(x, na.rm=TRUE)
# also fails because then R tries to execute the function
# > Error in mean(x, na.rm = TRUE) : Object 'x' not found
# Note: in this case, the custom +.aclass is not even called,
# but R calls the normal + method I think
我不确定如何解决这个问题,或者实际上是否可能...
谢谢你的帮助!
通过消除substitute
在add_fun
功能和返回self
(因为你是它影响到一个新的对象)
# this is the function adding part
add_fun = function(fun) {
self$fun <- fun
self
},
它将与
my_class <- Aclass$new(c(0,1,NA)) + foo
my_class <- Aclass$new(c(0,1,NA)) + function(x) mean(x,na.rm=TRUE)
对于您的目标案例,将更加困难
+ mean(x, na.rm=TRUE)
甚至对ggplot2函数都进行了评估(例如,labs(x="title")
将评估为添加到ggplot中的class label对象最终将调用function ggplot_add.labels
)
通过使用公式表示法稍微调整目标案例,可以解决问题
+ ~mean(x, na.rm=TRUE)
使用此代码
`+.aclass` <- function(e1, e2) {
se2 <- substitute(e2)
# detect formula
if(is.call(se2) && as.character(se2)=="~") {
# build template function
e3 <- substitute(function(x) y)
# replace y in template by formula body
e3[[3]] <- se2[[2]]
# use our templated formula function
e2 <- e3
}
e1$add_fun(e2)
e1
}
最后,使用rlang中可用的高级功能,我们可以将其简化为
`+.aclass` <- function(e1, e2) {
e1$add_fun(rlang::as_function(e2))
e1
}
请注意,类似函数的公式需要使用.x而不是x
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句