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

2024-05-01 Thread Kevin R. Coombes

Hi Simon,

Thanks for the detailed answer. I had considered all those 
possibilities, and was hoping that I had just missed something obvious.


I have been using S4 classes in my own packages for quite a while. But 
here, I didn't see any need to create my own class. Now, I may make my 
own, which presents a more abstract "graph" that can have realizations 
both as 'igraph' or "graphNEL' - 'Rargaph'. I'll have to think about, 
but that seems like the only reliable way to prevent putting al of 
Rgraphviz into the user's namespace.


Oh, and CRAN doesn't complain about importing Rgraphviz, since 
BioConductor is one of the "standard mainstream" repositories.


Best,
  Kevin

On 4/30/2024 11:46 PM, Simon Urbanek wrote:

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  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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel



__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


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

2024-04-30 Thread Simon Urbanek
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  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@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
> 

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


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

2024-04-30 Thread Kevin R. Coombes

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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel