[R-pkg-devel] re-exporting plot method?

Simon Urbanek @|mon@urb@nek @end|ng |rom R-project@org
Wed May 1 05:46:32 CEST 2024


Kevin,

welcome to the S4 world! ;) There is really no good solution since S4 only works at all if you attach the package since it relies on replacing the base S3 generic with its own - so the question remains what are your options to do it.

The most obvious is to simply add Depends: Rgraphviz which makes sure that any generics required are attached so your package doesn't need to worry. This is the most reliable in a way as you are not limiting the functionality to methods you know about. The side-effect, though, (beside exposing functions the user may not care about) is that such package cannot be on CRAN since Rgraphics is not on CRAN (that said, you mentioned you are already importing then you seem not to worry about that).

The next option is to simply ignore Rgraphviz and instead add setGeneric("plot") to your package and add methods to Depends and importFrom(methods, setGeneric) + exportMethods(plot) to the namespace. This allows you to forget about any dependencies - you are just creating the S4 generic from base::plot to make the dispatch work. This is the most light-weight solution as you only cherry-pick methods you need and there are no dependencies other than "methods". However, it is limited to just the functions you care about.

Finally, you could re-export the S4 plot generic from Rgraphviz, but I'd say that is the least sensible option, since it doesn't have any benefit over doing it yourself and only adds a hard dependency for no good reason. Also copying functions from another package opens up a can of worms with versions etc. - even if the risk is likely minimal.

Just for completeness - a really sneaky way would be to export an S3 plot method from your package - it would be only called in the case where the plot generic has not been replaced yet, so you could "fix" things on the fly by calling the generic from Rgraphviz, but that sounds a little hacky even for my taste ;).

Cheers,
Simon



> On 1/05/2024, at 6:03 AM, Kevin R. Coombes <kevin.r.coombes using gmail.com> wrote:
> 
> Hi,
> 
> I am working on a new package that primarily makes use of "igraph" representations of certain mathematical graphs, in order to apply lots of the comp sci algorithms already implemented in that package. For display purposes, my "igraph" objects already include information that defines node shapes and colors and edge styles and colors. But, I believe that the "graph" - "Rgraphviz" - "graphNEL" set of tools will produce better plots of the graphs.
> 
> So, I wrote my own "as.graphNEL" function that converts the "igraph" objects I want to use into graphNEL (or maybe into "Ragraph") format in order to be able to use their graph layout and rendering routines. This function is smart enough to translate the node and edge attributes from igraph into something that works correctly when plotted using the tools in Rgraphviz. (My DESCRIPTION and NAMESPACE files already import the set of functions from Rgraphviz necessary to make this happen.)
> 
> In principle, I'd like the eventual user to simply do something like
> 
> library("KevinsNewPackage")
> IG <- makeIgraphFromFile(sourcefile)
> GN <- as.graphNEL(IG)
> plot(GN)
> 
> The first three lines work fine, but the "plot" function only works if the user also explicitly includes the line
> 
> library("Rgraphviz")
> 
> I suspect that there is a way with imports and exports in the NAMESPACE to make this work without having to remind the user to load the other package. But (in part because the plot function in Rgraphviz is actually an S4 method, which I don't need to alter in any way), I'm not sure exactly what needs to be imported or exported.
> 
> Helpful suggestion would be greatly appreciated.
> 
> Best,
>   Kevin
> 
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
> 



More information about the R-package-devel mailing list