I am currently working on the R package and am trying to follow the best practice guidelines provided by Hadley Wickham at http://r-pkgs.had.co.nz . As part of this, I intend to use all the package dependencies in the “Import” section of the DESCRIPTION file, not the “Dependencies”, since I agree with the philosophy of necessity without unnecessarily changing the global environment (which many CRAN and Bioconductor packages seem to put on should).
I want to use the functions in the Bioconductor rhdf5 package as part of one of my package functions, in particular h5write() . The problem I am facing is that it does not have its S3 methods declared as such in its NAMESPACE. They are declared using (for example)
export(h5write.default) export(h5writeDataset.matrix)
but not
S3method(h5write, default) S3method(h5writeDataset, matrix)
Common h5write is defined as:
h5write <- function(obj, file, name, ...) { res <- UseMethod("h5write") invisible(res) }
In practice, this means that rhdf5 :: h5write calls fail because there is no corresponding h5write method registered.
As far as I can see, there are three options for this:
- Use
Depends , not Imports in the DESCRIPTION file. - Use
library("rhdf5") or require("rhdf5") in the code for the corresponding function. - Change the NAMESPACE file for rhdf5 to use
S3methods() rather than export() .
All these flaws. Option 1 means that the package is downloaded and connected to the global environment, even if the corresponding function in my package is never called. Option 2 means using library in the package, which at the same time binds the package to the global environment again and is also deprecated according to the recommendations of Hadley Wickham. Option 3 would mean relying on another package author to upgrade your package to Bioconductor, and also means that S3 methods are no longer exported, which in turn could break other packages that rely on them explicitly.
Did I miss another alternative? I looked at StackOverflow elsewhere and found the following few relevant questions Importing the S3 method from another package and How to export the S3 method, so is it available in the namespace? but nothing that directly relates to my problem. It should be noted that the key difference from the first of these two is that the common and the method are in the same package, but the problem is using export rather than S3method .
Sample code for reproducing the error (without creating a package):
loadNamespace("rhdf5") rdhf5::h5write(1:4, "test.h5", "test") Error in UseMethod("h5write") : no applicable method for 'h5write' applied to an object of class "c('integer', 'numeric')
Alternatively, there is a skeleton package at https://github.com/NikNakk/s3issuedemo , which provides a single demonstrateIssue() function that reproduces the error message. It can be installed using devtools::install_github("NikNakk/s3issuedemo") .