[R] identical function names from 2 packages

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri May 20 10:45:37 CEST 2011


On Fri, May 20, 2011 at 9:16 AM, Janko Thyson
<janko.thyson.rstuff at googlemail.com> wrote:

> Yet, IMHO there will be more and more problems regarding this in the
> future as the number of contributed packages keeps growing. I personally
> would not mind at all to get used to typing 'thePackage::foo()' *all*
> the time, or at least have this as an option. In principal, this is no
> big deal once you actually *have* a true package with a namespace at
> hand. But what about the process of creating a package? AFAU, there is
> no way of "simulating/emulating" a namespace (which I understand is some
> special form of environment?) in order to be able to use '::' when
> creating/debugging functions that call other functions of the unfinished
> package. If anyone has some ideas on that one, I would appreciate to hear.

 there's some stuff in utils that might do it for you:

Description:

     Utility functions to access and replace the non-exported functions
     in a name space, for use in developing packages with name spaces.

Usage:

     getFromNamespace(x, ns, pos = -1, envir = as.environment(pos))

     assignInNamespace(x, value, ns, pos = -1,
                       envir = as.environment(pos))

     fixInNamespace(x, ns, pos = -1, envir = as.environment(pos), ...)


See ?assignInNamespace for the full info.

Personally, I like the python way of doing things like this, and
dealing with name clashes. You can do:

 import foo

and then you do foo.bar(), foo.baz()

 or

from foo import bar

then you can just do bar(). There's also:

from foo import *

 which lets you do bar(), baz(), but is frowned upon a bit because of
possible name collisions. Or if you want to use functions with the
same name from two packages you can do:

from foo import bar as bar1
from baz import bar as bar2

 Then you can do bar1() and bar2()

 Of course if you want to change anything in the bar package then
similar hackery to the 'assignInNamespace' of R is necessary unless
you want to rebuild the python package. Often called "Monkey
patching", I think, although this possibly only refers to mucking with
class objects and methods.

Barry



More information about the R-help mailing list