简体   繁体   English

R使用s3和s4方法在同一个包中进行模拟

[英]R using s3 and s4 methods of simulate in the same package

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. 我编写了一个R包,其中包含一个simulate()方法的定义作为S3方法。 Because the generic for simulate is already defined, I simply define a simulate.myclass ( simulate.fitContinuous in my case). 因为模拟通用已经定义,我简单地定义一个simulate.myclass( simulate.fitContinuous在我的情况)。

The package also depends on another package that has an S4 version of simulate. 该软件包还依赖于具有S4版本模拟的另一个软件包。 When loading my package, I get the S4 version error above. 加载我的包时,我得到上面的S4版本错误。 I'm not sure what is producing the error. 我不确定是什么导致错误。

Reproducible example by grabbing the package from github , or do 从github抓取包的可重现的例子,或者做

require(devtools) 
install_github("pmc", "cboettig")
require(pmc)

To reproduce this error from scratch: Create a new package with minimal DESCRIPTION file. 从头开始重现此错误:使用最少的DESCRIPTION文件创建一个新包。 include the DESCRIPTION imports: ouch. 包括DESCRIPTION进口:ouch。 Create a NAMESPACE and add imports(ouch) and S3method(simulate, test). 创建一个NAMESPACE并添加导入(ouch)和S3method(模拟,测试)。 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): 创建R目录,添加一个简单的R脚本(我已经包含了生成我刚刚提到的NAMESPACE的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")
}

Install the package (document with devtools first if you like), and you get the error. 安装软件包(如果你愿意,首先使用devtools文件),你会收到错误。

My best solution so far is to eliminate the S3method line from the NAMESPACE, and export the full function simulate.test instead. 到目前为止,我最好的解决方案是从NAMESPACE中删除S3方法行,并导出完整的函数simulate.test。 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). 一个不同的解决方案是使用依赖和导入,并正确记录S3方法(如上所述)。 Then everything works as expected, but the warning message remains. 然后一切都按预期工作,但警告信息仍然存在。

simulate is an S3 generic defined in stats , so according to section 1.6.2 of "Writing R Extensions" (the example isn't clear -- there are exceptions for generics defined in base ) your NAMESPACE file should have simulatestats定义的S3泛型,因此根据“编写R扩展”的1.6.2节(示例不清楚 - base定义的泛型有例外),您的NAMESPACE文件应具有

importFrom(stats, simulate)
S3method(simulate, fitContinuous)

The 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 ). 关于“找到S4方法”的业务似乎反映了何时发现问题 - 尝试将S4方法添加到不可见的S3通用(“它”,我猜是指通用simulate )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM