[R] identical function names from 2 packages

Rolf Turner rolf.turner at xtra.co.nz
Fri May 20 09:28:51 CEST 2011


My understanding (which is pretty shaky when it comes to namespaces) is
that if you have things set up correctly then namespaces will make sure
that function calls within functions in the given package will be to be to
functions in that package and not to their doppelgangers in other packages
which may be attached.

E.g. if you have another function in the "teaser" package that calls lm(),
(and if you have the teaser namespace set up properly) it will call the
lm() in the teaser package and *not* the lm() in the "stats" package.

But a call to lm() from a function not in "teaser", or from the command
line will get the lm() in whichever package ("teaser" or "stats") is closer
to the start of the search path.  And there's really nothing much that
can be done about this.  Have I got that right?

     cheers,

             Rolf Turner
On 20/05/11 18:26, Jari Oksanen wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list