roxygen2 icon indicating copy to clipboard operation
roxygen2 copied to clipboard

NAMESPACE trouble with S3 methods

Open jgellar opened this issue 3 years ago • 4 comments

I have an R package that contains some S3 methods, where the generic functions are in another package (the mgcv package). More specifically, I create a few different methods for the generic functions smooth.construct and Predict.matrix. My package was working until recently, and I think the cause is something that roxygen is doing in generating the NAMESPACE file.

There are currently two different versions of the NAMESPACE that I've been trying, but both are giving me trouble.

The first version I think is the old version, which was working before. In the NAMESPACE file, it contains the following lines for these methods:

S3method(Predict.matrix,ar.smooth)
S3method(Predict.matrix,dt.smooth)
S3method(Predict.matrix,pi.smooth)
S3method(smooth.construct,ar.smooth.spec)
S3method(smooth.construct,dt.smooth.spec)
S3method(smooth.construct,pi.smooth.spec)

When I have this version, the package works fine locally (i.e., when I load it using devtools::load_all), but if I try to build the package I get the following error:

Error: package or namespace load failed for 'mgcvTrans':
 object 'Predict.matrix' not found whilst loading namespace 'mgcvTrans'
Error: loading failed

The other version of the NAMESPACE file I get occurs if I delete the file and then run devtools::document(). In this version, the lines above are replaced with:

export(Predict.matrix.ar.smooth)
export(Predict.matrix.dt.smooth)
export(Predict.matrix.pi.smooth)
export(smooth.construct.ar.smooth.spec)
export(smooth.construct.dt.smooth.spec)
export(smooth.construct.pi.smooth.spec)

This time, I am able to build the package, and devtools::check() runs with no errors. However, when I try to run the method smooth.construct.ar.smooth.spec, I get the following error:

Error in UseMethod("smooth.construct") : 
  no applicable method for 'smooth.construct' applied to an object of class "ar.smooth.spec"

What's strange is that I am able to use the other two smooth.construct methods without a problem. Also, as I mentioned before smooth.construct.ar.smooth.spec runs fine with the older version of NAMESPACE, when I run the package locally.

I've tried minimizing the roxygen code above smooth.construct.ar.smooth.spec so that it is just the @export tag, in order to eliminate any other potential issues, but it's not working.

Any suggestions on what could be going on?

jgellar avatar Aug 04 '22 19:08 jgellar

We can help you best if you show us an example that we can run. Is your package open source? Is it available at GitHub or elsewhere?

gaborcsardi avatar Aug 05 '22 08:08 gaborcsardi

Thanks for the response. Yes, you can find my package here: https://github.com/jgellar/mgcvTrans

It wasn't exactly accurate when I said the "new" version of the NAMESPACE uses export(), and the old version uses S3method(). I have generated both of them using the same version of roxygen2, and it's not clear to me which one is correct or how to get one versus the other.

jgellar avatar Aug 05 '22 13:08 jgellar

You'll need to import the mgcv generics into your package in order for the S3 method detection code to work correctly. We need to make this more clear in the docs.

hadley avatar Aug 05 '22 17:08 hadley

That worked, thanks. I had assumed this wouldn't be necessary because I am importing the whole mgcv package, so yes if the documentation could make this clearer it would be appreciated.

Thanks for your help!

jgellar avatar Aug 05 '22 17:08 jgellar