如果在方法中仅定义了S4泛型函数的命名参数,则substitute()将按预期工作:
> setGeneric("fS4", function(x, ...) standardGeneric("fS4"))
> setMethod("fS4", signature("numeric"),
+ function(x, ...) deparse(substitute(x))
+ )
[1] "fS4"
> fS4(iris[,1])
[1] "iris[, 1]"但是,如果在方法的定义中添加额外的命名参数,substitute()将无法在传递参数时正确返回该参数:
> setMethod("fS4", signature("numeric"),
+ function(x, y, ...) deparse(substitute(x))
+ )
[1] "fS4"
> fS4(iris[,1])
[1] "x"关于为什么会发生这种情况,以及最重要的是,如何解决它,有什么线索吗?
发布于 2012-11-23 02:55:54
看一看
showMethods(fS4, includeDef=TRUE)它显示了
Function: fS4 (package .GlobalEnv)
x="numeric"
function (x, ...)
{
.local <- function (x, y, ...)
deparse(substitute(x))
.local(x, ...)
}S4使用不同于泛型的签名实现方法的方式是,在具有泛型签名的函数内,创建一个具有修改后的签名的“.local”函数。然后,substitute会在不正确的环境中进行计算。根本问题与S4无关
> f = function(x) deparse(substitute(x))
> g = function(y) f(y)
> f(1)
[1] "1"
> g(1)
[1] "y"
> h = function(...) f(...)
> h(1)
[1] "1"在“正确”的环境中进行评估的任何尝试都将受到用户提供的任意构造的阻碍。
https://stackoverflow.com/questions/13517720
复制相似问题