Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 977
mpiktas Avatar asked Oct 25 '14 11:10

mpiktas


People also ask

Which function is import package in R?

import: An Import Mechanism for R The syntax allows for importing multiple objects from a package with a single command in an expressive way. The import package bridges some of the gap between using library (or require ) and direct (single-object) imports.


2 Answers

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.

like image 164
G. Grothendieck Avatar answered Sep 30 '22 17:09

G. Grothendieck


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
like image 32
mpiktas Avatar answered Sep 30 '22 17:09

mpiktas