Importing S3 Method from Another Package

Importing S3 method from another package

To summarise this as the original (below) is now out-dated and in places erroneous or misleading.

The proximal issue is that there is no function named predict in the pls package; there are some unexported S3 methods for predict but no such predict. So you can't import this. The predict generic lives in the stats package and you'll need to import from there as discussed below.

Your package needs to have Depends: pls in the DESCRIPTION in order for the correct predict method to be available to R. There's nothing in pls that you can specifically import.

You also need to import the predict generic from the stats namespace, so add

#' @importFrom stats predict

as that will import the generic in you packages namespace. You will also want to add Imports: stats to your DESCRIPTION file to indicate that you need the stats package; previously we didn't have to declare dependencies on the set of base packages shipped with R (i.e. the non-Recommended ones that ship with R).


Original

The main issue here is the pls doesn't define a function/method predict. It provides several methods for the predict generic, but not the generic itself.

You need to import the generic from the stats package, if you need it - I'm not sure you do as you aren't creating a function that needs or builds on the generic. At the minimum you'll need

#' @importFrom stats predict

though you may need/want to import the entire stats namespace - depends what your package does beyond the function your are currently working on.

The other issue is that predict.mvr is not exported from the pls namespace

> require(pls)
Loading required package: pls

Attaching package: ‘pls’

The following object(s) are masked from ‘package:stats’:

loadings

> predict.mvr
Error: object 'predict.mvr' not found
> pls::predict.mvr
Error: 'predict.mvr' is not an exported object from 'namespace:pls'
> pls:::predict.mvr
function (object, newdata, ncomp = 1:object$ncomp, comps, type = c("response",
"scores"), na.action = na.pass, ...)

As such you can't just import it. Hence your package needs to have Depends: pls in the DESCRIPTION in order for the correct predict method to be found.

How to use S3 methods from another package which uses export rather than S3method in its namespace without using Depends or library()

The key here is to import the specific methods in addition to the generic you want to use. Here is how you can get it to work for the default method.

Note: this assumes that the test.h5 file already exists.

#' @importFrom rhdf5 h5write.default
#' @importFrom rhdf5 h5write
#' @export
myFun <- function(){
h5write(1:4, "test.h5", "test")
}

I also have put up my own small package demonstrating this here.

Importing a package's S3 methods without importing its functions

Iñaki Úcar's mention of the @rawNamespace tag led me to work out a version that doesn't import any of package B's exported functions, using the getNamespaceExports function mentioned in this answer:

#' @rawNamespace import(packageB, except = getNamespaceExports("packageB"))

The @rawNamespace tag in roxygen2 inserts raw code into the NAMESPACE file. getNamespaceExports returns the names of all exported functions in a namespace: this can be a package that you haven't attached.

For my specific example, I can write this:

#' @import simmer
#' @rawNamespace import(simmer.plot, except = getNamespaceExports("simmer.plot"))

which puts these lines in the NAMESPACE:

import(simmer)
import(simmer.plot, except = getNamespaceExports("simmer.plot"))


Related Topics



Leave a reply



Submit