importAs

This package helps mimic the idiomatic python importing of packages such as

In order to mimic this as closely as possible, the function importAs takes a package name and a (short?) name for that and creates a linkage

or

This is merely a convenience, however. In reality a variable d is created with the value "dplyr" (and a class so we can dispatch on it). We could also do this manually for another package, creating a simple character object

The importAs function is merely a signal to a reader that this magic is going to be performed. The class means it is clearer that the shorthand is being used.

A custom description is provided within the RStudio Environment pane which identifies each shorthand and the namespace to which it refers.

As a further convenience, this can be specified using an infix operator (h/t @sa-lee)

At this point nothing magical has happened. The magic comes from overwriting the :: operator. To – at least, my – surprise, :: can happily take a character argument for the left-hand side, so

works. The magic part is rewriting this function to look deeper if provided with a symbol which already resolves to a character string representing an installed package. The result is that we can shorthand the namespace referencing

without interfering with any existing functionality

Collisions

If the symbol you are trying to use as a shorthand already exists as an available namespace, then that will take precedence. This prevents accidentally overwriting a namespace reference

and the existing namespace will still be found

Installation

You can install the development version of importAs with