【发布时间】:2014-02-20 19:04:42
【问题描述】:
当我在 R 2.14.1 中创建 s3 方法,然后调用它们时,如果方法与已加载到工作区的函数(即基本函数)同名,则 s3 对象无法执行这些方法。相反,它调用基本函数并返回错误。此示例使用“匹配”。在今天之前我从来没有遇到过这个问题。自从我上次运行此代码以来,我安装了 R 3.0.2,但保留了我的 2.14.1 版本。由于某些软件包在 CRAN 中不是最新的,我在 3.0.2 中遇到了一些麻烦(不同的麻烦),所以我将 RStudio 恢复到 2.14.1,然后这个问题突然出现了。这是一个例子:
rm(list=ls())
library(R.oo)
this.df<-data.frame(letter=c("A","B","C"),number=1:3)
setConstructorS3("TestClass", function(DF) {
if (missing(DF)) {
data= NA
} else {
data=DF
}
extend(Object(), "TestClass",
.data=data
)
})
setMethodS3(name="match", class="TestClass", function(this,letter,number,...){
ret = rep(TRUE,nrow(this$.data))
if (!missing(number))
ret = ret & (this$.data$number %in% number)
if (!missing(letter)){
ret = ret & (this$.data$letter %in% letter)
}
return(ret)
})
setMethodS3("get_data", "TestClass", function(this,...) {
return(this$.data[this$match(...),])
})
hope<-TestClass(this.df)
hope$match()
Error in match(this, ...) : argument "table" is missing, with no default
hope$get_data()
这里是 sessionInfo() 的线索:
sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] R.oo_1.13.0 R.methodsS3_1.4.2
loaded via a namespace (and not attached):
[1] tools_2.14.1
我尝试了很多 setMethodsS3 中的参数组合,但没有成功。
我该如何解决这个问题?
【问题讨论】: