[R] identical function names from 2 packages

Jari Oksanen jari.oksanen at oulu.fi
Fri May 20 08:26:00 CEST 2011


Duncan Murdoch <murdoch.duncan <at> gmail.com> writes:

> 
> On 18/05/2011 10:02 PM, Nick Matzke wrote:
> > Hi,
> >
> > If I load 2 packages that have a function with the same
> > name, how do I tell R to run one or the other?
> >

> 
> If you are using a package without a namespace, expect problems.  Having 
> the namespace is nearly as good as automatically adding the pkg:: prefix 
> to every call from functions within the package.  Not having the 
> namespace is nearly as bad as never using the prefix, even when you should.
> 
I fail to see how namespace could help here. If you have identical function
names in two packages, one will still mask another and you'll need an explicit
pointer (::) to tell R which one to use. Here is an example:

> lm(y ~ x)
Error in lm(y ~ x) : got you!
> lm
function (x, ...) 
stop("got you!")
<environment: namespace:teaser>

So there is an unreleased (fortunately!) package called "teaser" 
which has only function called lm() that seems to do nothing useful, but
it masks the more useful function lm() in package "stats".  Both packages
("teaser" and "stats") have namespace, but it didn't help here.

Namespace may be useful when you got stray objects from two different
packages, and these should be handled by their dedicated support functions,
but I fail to see how namespace could help in resolving which of the two exported
functions to use at the top level.

I have honest intentions in this query since I know that the package that I
maintain has a function with the same name as another package that is often
used alternately with our package. My resolution was to write a function that 
detects the case and gives user a warning that the object they have was
created from that another package and cannot be adequately handled here.
However, it doesn't remove the confusion when people think they are using
one package but use another when doing the top level analysis.

If there is a resolution to this, I'll be happy.

Cheers, Jari Oksanen



More information about the R-help mailing list