I'm working with two R packages (network and sna) that are essentially 'peers': they Suggest each other and supply compatible and related functionality. Unfortunately, they both define the '%c%' operator, so loading the second package gives a warning:
The following object is masked from ‘package:sna’: %c%
This seems like perfect job for S3 generics-- the network package can define %c%.network
, sna can define %c%.matrix
, with the dispatching handled appropriately by a %c%
generic. The problem is, since both packages need to work independently, how do I define and export the generic to avoid the warning?
I've tried putting code like the following in both NAMESPACE files so that whichever loads first can define the generic:
if (!exists('%c%')){
export(`%c%`)
}
but it doesn't seem to work. What is the correct way to handle this sort of conditional function definition and namespace export?
Edit: Did a little digging into the parsing of R NAMESPACE file in the R src (https://svn.r-project.org/R/branches/R-3-1-branch/src/library/base/R/namespace.R). Seems like 'if' is supported, and its argument should be eval()'d, but I guess this must happen in an environment that the functions loaded by namespace are not yet attached to?