3

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?

skyebend
  • 1,079
  • 6
  • 19
  • Some resources: http://stackoverflow.com/questions/8474872/safely-creating-s3-generics-in-r http://cran.r-project.org/web/packages/R.methodsS3/index.html https://github.com/cran/R.methodsS3 – Gabor Csardi Aug 11 '14 at 22:02
  • If you want a generic to be used by both packages, you need to create a package that defines the generic and is imported by both packages. – hadley Aug 12 '14 at 12:17
  • @hadley I think CRAN will frown upon us creating a package with only a single method. – skyebend Aug 14 '14 at 22:18
  • @GaborCsardi looked into R.methodsS3 internals to see how it does it. Seems to use assign() to conditionally generate a generic, but perhaps doesn't add it to the namespace? – skyebend Aug 14 '14 at 22:31
  • A package with a single function is fine, and I believe it's the right way to solve this problem. – hadley Aug 21 '14 at 14:00

0 Answers0