You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Defining generic methods for suggested packages #573
When building a package that is exporting a generic function, what is the proper way to export a method for a class within a "Suggests" package?
For example, if package foo exports the foo() generic, but lists package bar as a Suggests, what would be the proper way to register methods for classes within bar?
Would it be something to the effect of:
# from the foo package.onLoad<-function(lib, pkg) {
if (isNamespaceLoaded("bar")) {
method(foo, bar::class_bar) <-function(x) {cat("we dispatched on `bar::class_bar`")}
} else {
setHook(packageEvent("bar", "onLoad"), function(){
method(foo, bar::class_bar) <-function(x) {cat("we dispatched on `bar::class_bar`")}
})
}
}
This is not quite what I am looking for and a bit opposite for my use case.
The link describes how to conditionally register external generics for classes within your own package.
For my use case, the foo generic exists in my own package I want to conditionally provide a method for an external class bar depending on if it is loaded or may get loaded in the future.
I think your approach with onLoad is reasonable, until we come up with something better. Just one thing to note is that you should call the setHook() unconditionally; this protects from (the admittedly rare case) where the user unloads then reload the package. I'd also extract out a function to reduce the duplication:
Activity
teunbrand commentedon Oct 14, 2025
I think the advice for external generics is written up here: https://rconsortium.github.io/S7/reference/new_external_generic.html
jtlandis commentedon Oct 14, 2025
This is not quite what I am looking for and a bit opposite for my use case.
The link describes how to conditionally register external generics for classes within your own package.
For my use case, the
foogeneric exists in my own package I want to conditionally provide a method for an external classbardepending on if it is loaded or may get loaded in the future.hadley commentedon Oct 15, 2025
I think your approach with onLoad is reasonable, until we come up with something better. Just one thing to note is that you should call the
setHook()unconditionally; this protects from (the admittedly rare case) where the user unloads then reload the package. I'd also extract out a function to reduce the duplication:jtlandis commentedon Oct 15, 2025
hadley commentedon Oct 15, 2025
Unconditionally register a on-load hook (#635)
Implement external classes (RConsortium#660)