【发布时间】:2017-03-18 22:21:21
【问题描述】:
看看这个“简单”的功能:
test <- function(x,...){
UseMethod("test",x)
}
test.default<-function(x,y,data){
message("default")
print(deparse(substitute(x)))
print(deparse(substitute(y)))
print(deparse(substitute(data)))
print(match.call())
}
test.formula <- function(x,...){
message("formula")
print(deparse(substitute(x)))
print(match.call())
}
一切都好
data(iris)
test.formula(Sepal.Length~Petal.Width,iris)
test.default(Sepal.Length,Petal.Width,iris)
test(Sepal.Length~Petal.Width,iris)
除了这个:
test(Sepal.Length,Petal.Width,iris)
因为 NSE:object 'Sepal.Length' not found
有什么想法吗?
【问题讨论】:
-
我没有收到错误,第三个“测试”返回与第一个相同。除非您的函数提供
iris对象作为环境,否则我不希望 R 能够找到列名。 -
@42- 你试试这个:
test(Sepal.Length,Petal.Width,iris)? -
是的,但
Sepal.Length不是搜索路径上的 R 名称。它是iris的一个属性,带有一个字符值。 -
@42- 错误不是
test(Sepal.Length~Petal.Width,iris)而是test(Sepal.Length,Petal.Width,iris),我知道为什么我有错误(因为NSE),我只想找到一个“不错”的使用方式带有 UseMethod 的 NSE。