Is there a way to import a package with another name in R, the way you might with import as
in Python, e.g. import numpy as np
? I've been starting to use package::function
lately to avoid conflicts between, say, Hmisc::summarize
and plyr::summarize
.
I'd like to be able to instead write h::summarize
and p::summarize
, respectively. Is this possible in R?
Rather than aliasing the package, why not just alias the function?
I was starting down an
eval(parse())
path, but I ran into trouble and need to get back to work. @Thomas's answer seems to get a similar result in a much smoother way, but here's the non-working draft.With the idea that you could do something like
to create
p..ddply
, etc.Too long to fit nicely in comment box, so pseudo-answer:
If it's only a few (or few dozen) functions, how about an override wrapper function, e.g.
Here's a solution that should only be used for interactive mode. You modify
::
so that it can accept character package names, then write a function to register the aliases.But instead of using aliases, you could also redefine
::
to find the package that matches your abbreviation:This is not quite what you want because it involves changing from
::
notation to$
notation, but if you load a package namespace (without attaching it), you can then refer to it by its environment name:Note, however, that you do lose access to documentation files using the standard
?
notation (e.g.,? p$summarise
does not work). So, it will serve you well as shorthand, but may not be great for interactive use since you'll still have to resort to? plyr::summarise
for that.Note also that you do not have access to the data objects stored in the package using this approach.
Use the namespace package to generate another namespace aliased to the one you are interested in.
Note this has the disadvantage of making the package versioning/installation requirements more opaque for scripts.