Skip to content
Prev 36141 / 63424 Next

Defining a method in two packages

On Sat, 6 Mar 2010, Uwe Ligges wrote:

            
But the packages could be more cooperative.  There is also a S3 
generic for ranef in package nlme.  Ideally the S4 generic in lme4 
would have a default method which dispatched on S3 methods: that would 
be easy to achieve if there was just one other ranef, but not so if 
there are 2 or more -- but read on.

Indeed, the situation is pretty much the same if you load nlme after 
coxme:
Attaching package: 'nlme'

The following object(s) are masked from 'package:coxme':

     fixed.effects, fixef, random.effects, ranef
Error in UseMethod("ranef") :
   no applicable method for 'ranef' applied to an object of class "coxme"

The situation can be alleviated by making S3 methods visible.  Thus if
coxme exported coxme:::ranef.coxme and lme4 had a default method

function (object, ...) UseMethod("ranef")

loading lme4 (or nlme) after coxme would work.

My solution would though be NOT to reuse a name that is already 
established in another package (nlme has used it for many years).

The design problem is that generic foo() in package B might have 
nothing to do with foo() in package A.  When it does, we expect B to 
import the generic from A, but that can be a 'which came first' 
problem unless a base package is involved.  But here nlme is a 
long-established recommended package, so it would be reasonable to 
expect both coxme and lme4 to import from it.