1

I'm trying to figure out if in R, it would be possible to implement the python-ism

import VeryLongPackageName as alias

(This essentially only serves to save keystrokes, but I think if it were easier to fully-qualify packages, there would be less painful masking to deal with in our lives.)

So far, I see that the package::object operator calls getExportedValue(as.character(package), as.character(value)), which in turn eventually calls getNamespace(as.character(package)). This either tries an internal C function or failing that calls loadNamespace. And there I am stuck. I have not found any documentation about how to do reflection onto the registry of namespaces.

Alternatively, I wonder if it might be possible to overload the :: operator, though I can't tell if this is a terrible idea, or not.

Andrew M
  • 490
  • 5
  • 11
  • 1
    Suggested duplicate: ["import as" in R](https://stackoverflow.com/q/24391251/903061) – Gregor Thomas Oct 18 '18 at 19:28
  • 1
    Since there does not appear to be a "robust" way to do it (I'd be interested, too!), the linked answer's warning that this *"should only be used for interactive mode"* should be extended to read *"will produce unreproducible code"*, for anybody who does not also use this hack. – r2evans Oct 18 '18 at 19:36
  • Also related: https://stackoverflow.com/questions/24742927/how-to-manually-create-and-load-namespaces-in-r – Andrew M Oct 18 '18 at 20:54
  • I feel that the SO question I posted above best answers my question (as least as titled). With it's a one-liner now to register an alias for a package namespace: `library(namespace); registerNamespace('ggp', loadNamespace('ggplot2'))` makes all ggplot exports available with `ggp::` – Andrew M Oct 18 '18 at 21:09
  • Yeah, that's a nice solution. You should add it as an answer to the marked dupe so more people can find it. – Gregor Thomas Oct 19 '18 at 19:37

0 Answers0