如何从另一个包导入R函数,以便它可供用户使用?

时间:2022-02-24 09:20:44

I am writing a R package and I want to import generic function forecast from the package forecast. My package provides the method forecast.myobj. I have the forecast in the Imports: in the package DESCRIPTION file and my function definition is as following:

我正在写一个R包,我想从包预测中导入泛型函数预测。我的包提供了方法forecast.myobj。我在Imports中有预测:在包描述文件中,我的函数定义如下:

##' @export
forecast.myobj <- function(x) {

}

I am using devtools package (version 1.5) to build the package. The generated NAMESPACE has the following

我正在使用devtools包(版本1.5)来构建包。生成的NAMESPACE具有以下内容

S3method(forecast, myobj)
importFrom(forecast, forecast)

However when I load my package in a clean R session, function forecast is not available. Interestingly though I can see help pages of forecast and forecast.myobj and I can access these functions via forecast::forecast and mypackage:::forecast.myobj. Is it possible somehow to make forecast available to the user without depending on the package forecast? I checked documentation and reviewed a bunch of similar questions here, but I did not find the definite negative or positive answer.

但是,当我在干净的R会话中加载我的包时,功能预测不可用。有趣的是,虽然我可以看到预测和forecast.myobj的帮助页面,我可以通过forecast :: forecast和mypackage ::: forecast.myobj访问这些功能。是否有可能在不依赖于软件包预测的情况下以某种方式向用户提供预测?我检查了文档,并在这里回顾了一堆类似的问题,但我没有找到明确的否定或肯定的答案。

2 个解决方案

#1


10  

The imported function must be exported in the NAMESPACE file for it to be available to users:

导入的函数必须在NAMESPACE文件中导出才能供用户使用:

S3method(forecat, myobj)
importFrom(forecast, forecast)
export(forecast)

For an example, see the dplyr package's NAMESPACE file which imports %>% from the magrittr package and exports it so that it is accessible by the user.

有关示例,请参阅dplyr软件包的NAMESPACE文件,该文件从magrittr软件包导入%>%并将其导出,以便用户可以访问它。

#2


5  

Giving my own answer to add information how to achieve the NAMESPACE described in @G. Grothendieck's answer using devtools package. The following lines (modeled after dplyr's code) do the trick

给出我自己的答案,添加如何实现@G中描述的NAMESPACE的信息。 Grothendieck使用devtools包的答案。以下几行(以dplyr的代码为模型)可以解决问题

##' @importFrom forecast forecast
##' @name forecast
##' @rdname forecast.myobj
##' @export
NULL

#1


10  

The imported function must be exported in the NAMESPACE file for it to be available to users:

导入的函数必须在NAMESPACE文件中导出才能供用户使用:

S3method(forecat, myobj)
importFrom(forecast, forecast)
export(forecast)

For an example, see the dplyr package's NAMESPACE file which imports %>% from the magrittr package and exports it so that it is accessible by the user.

有关示例,请参阅dplyr软件包的NAMESPACE文件,该文件从magrittr软件包导入%>%并将其导出,以便用户可以访问它。

#2


5  

Giving my own answer to add information how to achieve the NAMESPACE described in @G. Grothendieck's answer using devtools package. The following lines (modeled after dplyr's code) do the trick

给出我自己的答案,添加如何实现@G中描述的NAMESPACE的信息。 Grothendieck使用devtools包的答案。以下几行(以dplyr的代码为模型)可以解决问题

##' @importFrom forecast forecast
##' @name forecast
##' @rdname forecast.myobj
##' @export
NULL