How to conditionally define a common function in R NAMESPACE

I am working with two R packages ( network and sna ), which are essentially "peers": they offer each other and provide 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 an ideal job for generic S3 files: the network package can determine %c%.network

, sna can determine %c%.matrix

, and dispatch is handled accordingly %c%

. The problem is that since both packages have to work independently, how do I define and export common code to avoid the warning?

I tried to put the code like the following in both NAMESPACE files so that depending on which loads can determine the common one first:

if (!exists('%c%')){ 
  export(`%c%`)
}

      

but it doesn't seem to work. What is the correct way to handle such a conditional function definition and export the namespace?

Edit: Dug a little deeper into parsing the R NAMESPACE file in R src ( https://svn.r-project.org/R/branches/R-3-1-branch/src/library/base/R/namespace.R ). It seems like "if" is supported and its argument should be eval () 'd, but I'm guessing this should happen in an environment where the namespace loaded functions are not yet bound to?

+3


source to share





All Articles