如何从另一个软件包扩展S3方法而不加载该软件包 [英] How to extend S3 method from another package without loading the package
问题描述
我正在开发一个具有功能 forecast.myclass
的软件包。我希望该功能与 forecast
包一起正常工作。即加载 forecast
程序包时,代码 forecast(object)
应该调用 forecast.myclass
从我的包裹中。
I am developing a package which has the function forecast.myclass
. I want that function to work nicely with forecast
package. I.e. when forecast
package is loaded the code forecast(object)
should call forecast.myclass
from my package.
因为我只需要包 precast $ c中的
forecast
的通用定义$ c>,并且我不使用包 forecast
中的任何其他函数,因此我不愿意将其包含在Depends中。因此,我通过以下方式在我的程序包中定义了泛型:
Since I need only generic definition of forecast
from the package forecast
, and I do not use any other function from the package forecast
I am reluctant to include it in the Depends. So I define the generic in my package in the following way:
##'
##' @export
forecast <- function(object,...) UseMethod("forecast")
##' @rdname forecast.midas_r
##' @method forecast midas_r
##' @export
forecast.midas_r <- function(object,newdata=NULL,method=c("static","dynamic"),insample=get_estimation_sample(object),...) {
现在,当软件包 forecast
时,一切都按预期工作未加载。但是当我加载软件包 forecast
时,在进行 forecast时不会调用
,其中 forecast.midas_r
(对象) object
属于类 midas_r
。我应该如何解决这个问题?
Now everything works as expected when package forecast
is not loaded. But when I load package forecast
, then forecast.midas_r
is not called, when doing forecast(object)
where object
is of class midas_r
. How should I solve this problem?
推荐答案
我不确定是否有简单的解决方案。正如其他人所说,使用 Depends
解决此问题,而不是重新定义通用方法,可能最简单。
I'm not sure there's an easy solution to this. As others have said it's probably easiest to use Depends
to get around this, rather than redefining a generic method.
这是一个对我有用的简单示例。它与您的解决方案基本相同,但是声明 @export
意味着您无需手动更新 NAMESPACE
文件
Here's a simple example which works for me. It's largely the same as your solution, but declaring @export
means you won't need to manually update the NAMESPACE
file.
##' @name mean
##' @export mean.newClass
##'
##' @method mean newClass
##'
##' @title mean for \code{newClass} object
##' @param x A \code{newClass} object
##' @param ... Additional arguments
##'
mean.newClass <- function(x, ...){
stopifnot(class(x)=="newClass")
return(42)
}
然后 package.skeleton( newPkg)
。将具有以上内容的文件 mean.R
放入软件包的目录 / R
中。
Then package.skeleton("newPkg")
. Put file mean.R
with the above contents in the directory /R
of the package.
确保您位于目录1下一级,然后
Ensure you're in the directory 1 level below, then
roxygenize("newPkg", roxygen.dir="newPkg", copy.package=F, unlink.target=F)
现在
library(devtools)
dev_mode(on=TRUE) ### don't want to have to uninstall the package later
install_local("newPkg")
library(newPkg)
x <- c(1,2)
class(x) <- "newClass"
stopifnot(mean(x)==42)
stopifnot(mean(unclass(x))==1.5)
我意识到平均值
是 base
中的一个函数,但是我已经测试过此方法可以修改泛型函数在其他地方为他们提供一种新方法,因此它也应扩展到您更一般的情况。
I realize mean
is a function in base
but I have tested this for modifying generic functions elsewhere to give them a new method, so it should extend to your more general case also.
这篇关于如何从另一个软件包扩展S3方法而不加载该软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!