[R] identical function names from 2 packages
Greg Snow
Greg.Snow at imail.org
Fri May 20 22:56:23 CEST 2011
>From the command line the namespaces may not help much (I am happy to be corrected). But if I am running a function from the command line then I can check the search path to see if there is a conflict and make sure to call the correct one, the bigger problem is when writing another package that the user of the new package may have loaded other packages or packages in a different order.
If I am writing my own package that calls one of the lm functions, then I can include a namespace for my package and in the namespace I can give it information on other package namespaces to import. So if my package namespace points to teaser's namespace, then any call to lm in my package will use the teaser version of lm rather than stats, regardless of their relative positions on the search path.
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rolf Turner
> Sent: Friday, May 20, 2011 1:29 AM
> To: Jari Oksanen
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] identical function names from 2 packages
>
>
> 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.
> >
>
> ______________________________________________
> 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