-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing methods for class defined in separate package #526
Comments
Exporting the class (constructor) signals that you're happy to have other people extend the class or register new methods. Not-exporting it, signals the opposite. So it sounds like you should just export the class? |
I agree that exporting the constructor is the easiest solution. However, I'm running into issues with R CMD CHECK when trying to document the class. My actual use case is here: I've documented all the elements of the class, and the docs appear to be rendering correctly (e.g., the pkgdown site). But I keep getting this warning from when using
The is one other S7 class that I'm also exporting, and it does not raise any issues (https://github.com/r-dcm/dcmstan/blob/16bc0e1eea324c3cd7db1efec1aad9baa138b9a9/R/zzz-class-model-priors.R#L120-L191). The only difference I can see is that class causing an issue has properties that are also S7 classes, whereas the non-problematic class uses only base classes for the properties. Any ideas on how to resolve the R CMD CHECK warning for classes that use other S7 classes as properties? |
I have a package, testpkgA, which defines a new S7 class,
pkga_fit
, along a generic and a method for the class. Critically, the class constructor is not exported:https://github.com/wjakethompson/testpkgA/blob/d5a36100acfe5dd797ea36125ef07a22a5957559/R/zzz-class-pkga_fit.R#L1-L18
Rather, a separate function is exported, which calls the constructor. A practical example may be modeling packages. Users may call a function that takes in data and estimates a model, and which then returns a fitted model object. We wouldn't expect the user to access the fitted model constructor on their own. Here is the "estimator" function from the example package:
https://github.com/wjakethompson/testpkgA/blob/d5a36100acfe5dd797ea36125ef07a22a5957559/R/fit-mod.R#L1-L17
Within the package, everything works as intended. I can run
fit_model()
, which returns apkga_fit
object, and I can successfully use the new generic,converged()
, which has a method defined for thepkga_fit
class.Created on 2025-02-10 with reprex v2.1.1
No consider that for one reason or another (e.g., I don't own testpkgA), that I want to write a new generic and/or method for the
pkga_fit
class. In testpkgB, I write the following code to create the generic and method:https://github.com/wjakethompson/testpkgB/blob/160b9ea254f3f38f491ba369ed679af197a5b5d1/R/zzz-method-model_name.R#L1-L17
However, this fails no matter how I try and specify the class for the method:
Created on 2025-02-10 with reprex v2.1.1
Is there a way to write an S7 method for a class that isn't directly exported from another package?
The text was updated successfully, but these errors were encountered: