R在同一个包中使用s3和s4方法进行模拟
我对我编写的 R 包中的错误感到困惑
found an S4 version of 'simulate' so it has not been imported correctly
,该包包含将 Simulate() 方法定义为 S3 方法。因为模拟的泛型已经定义,所以我只需定义一个simulate.myclass (
该软件包还依赖于另一个具有 S4 版本模拟的软件包。加载我的包时,我收到上面的 S4 版本错误。我不确定是什么产生了错误。
通过获取来自github的包来重现示例,或者执行
require(devtools)
install_github("pmc", "cboettig")
require(pmc)
重现此错误从头开始:使用最少的描述文件创建一个新包。包括进口描述:哎呀。创建一个命名空间并添加导入(ouch)和S3method(模拟,测试)。创建 R 目录,添加一个简单的 R 脚本(我已经包含了 roxygen 文档,它将生成我刚才提到的命名空间,但在没有 devtools/roxygen 的情况下也可能会创建此错误):
#' simulate
#'
#' a test for s3/s4 conflicts
#' @param object who cares?
#' @param nsim guess.
#' @param seed yup
#' @param ... other parameters we will just ignore
#' @return something
#' @method simulate test
#' @S3method simulate test
#' @import ouch
simulate.test <- function(object, nsim = 1, seed = NULL, ...){
message("This test worked")
}
安装软件包(首先包含 devtools 的文档如果你愿意的话),然后你会得到错误。
到目前为止,我最好的解决方案是从命名空间中删除 S3method 行,并导出完整的函数simulate.test。这将通过检查并安装而不会发出警告,但显然是一个较差的解决方案。
一个不同的解决方案是在依赖和导入中添加 ouch,并正确记录 S3 方法(如上所述)。然后一切都按预期进行,但警告消息仍然存在。
I'm puzzled by the error
found an S4 version of 'simulate' so it has not been imported correctly
I have written an R package that includes a definition for a simulate() method as an S3 method. Because the generic for simulate is already defined, I simply define a simulate.myclass (simulate.fitContinuous in my case).
The package also depends on another package that has an S4 version of simulate. When loading my package, I get the S4 version error above. I'm not sure what is producing the error.
Reproducible example by grabbing the package from github, or do
require(devtools)
install_github("pmc", "cboettig")
require(pmc)
To reproduce this error from scratch: Create a new package with minimal DESCRIPTION file. include the DESCRIPTION imports: ouch. Create a NAMESPACE and add imports(ouch) and S3method(simulate, test). Create the R directory, add the a trivial R script (I've included roxygen documentation that will generate the NAMESPACE I've just mentioned, but this error can also be created without devtools/roxygen):
#' simulate
#'
#' a test for s3/s4 conflicts
#' @param object who cares?
#' @param nsim guess.
#' @param seed yup
#' @param ... other parameters we will just ignore
#' @return something
#' @method simulate test
#' @S3method simulate test
#' @import ouch
simulate.test <- function(object, nsim = 1, seed = NULL, ...){
message("This test worked")
}
Install the package (document with devtools first if you like), and you get the error.
My best solution so far is to eliminate the S3method line from the NAMESPACE, and export the full function simulate.test instead. This will pass check and install without warnings, but is clearly an inferior solution.
A different solution is to have ouch in depends as well as imports, and document the S3 method properly (as above). Then everything works as expected, but the warning message remains.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
simulate
是在stats
中定义的 S3 泛型,因此根据“编写 R 扩展”的第 1.6.2 节(示例不清楚 - 泛型有例外)在base
中定义)您的 NAMESPACE 文件应该包含有关“找到 S4 方法”的内容似乎反映了问题何时被发现 - 尝试将 S4 方法添加到不可见的 S3 泛型(这个“它”我猜测指的是通用
模拟
)。simulate
is an S3 generic defined instats
, so according to section 1.6.2 of "Writing R Extensions" (the example isn't clear -- there are exceptions for generics defined inbase
) your NAMESPACE file should haveThe business about "found an S4 method" seems to reflect when the problem was discovered -- trying to add S4 methods to an S3 generic that wasn't visible (the "it" I guess refers the the generic
simulate
).