knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

importAs

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

import dplyr as d

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

library(importAs)
importAs(dplyr, d)
d

or

importAs("dplyr", "d")
d

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

s <- "stringr"
s

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)

dplyr %importAs% d
d

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

## equivalent to "dplyr"::filter
base::`::`("dplyr", filter)

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

d::filter(mtcars, cyl == 4, am == 1)
s::str_extract("a1b2c3", "[a-z]2")

without interfering with any existing functionality

dplyr::filter(mtcars, cyl == 4, am == 1)
stringr::str_extract("a1b2c3", "[a-z]2")

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

importAs(stringr, dplyr)
dplyr::str_extract

and the existing namespace will still be found

dplyr::select

Debugging

If you want to be clear about where your shorthands point to, you can set the debug option

options(importAs.debug = TRUE)

which will turn on messaging whenever a shorthand is accessed. For example, using dplyr normally does not

dplyr::filter(mtcars, cyl == 4 & am == 1)

but using the shorthand, we are reminded as to where that refers

d::filter(mtcars, cyl == 4 & am == 1)
options(importAs.debug = FALSE)

Installation

You can install the development version of importAs with

## install.packages("devtools")
devtools::install_github("jonocarroll/importAs")


jonocarroll/importAs documentation built on May 15, 2019, 1:16 p.m.