mpiktas
mpiktas

Reputation: 11518

How to import an R function from another package such that it would be available for the user?

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:

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

}

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

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.

Upvotes: 13

Views: 1983

Answers (2)

mpiktas
mpiktas

Reputation: 11518

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

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

Upvotes: 12

G. Grothendieck
G. Grothendieck

Reputation: 270438

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

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.

Upvotes: 12

Related Questions