Easily load and install multiple packages from different sources, including CRAN and GitHub. The libraries function allows you to load or attach multiple packages in the same function call. The packages function will load one or more packages, and install any packages that are not installed on your system (after prompting you). Also included is a from_import function that allows you to import specific functions from a package into the global environment.
easypackages package makes it easy to load or install multiple packages in R. Basically, it aims to solve two small day-to-day problems faced by R users:
libraryonce for each additional package that you wish to use
These problems are solved with the
packages functions, respectively. Read on to learn more.
At its most basic, the
libraries function allows this:
to become this:
libraries("dplyr", "ggplot2", "RMySQL", "data.table")
The functions in this package purposefully do not use non-standard evaluation, this package names must either be strings or variables pointing to strings (or objects of class
package_obj, see below for more). Not using non-standard evaluation allows us to do things like this:
my_packages <- c("dplyr", "ggplot2", "RMySQL", "data.table")libraries(my_packages)
packages function looks for packages that are not currently installed and installs them after confirming that this is OK with the user (this behavior may be turned off, but is enabled by default).
packages("dplyr", "ggplot2", "RMySQL", "data.table")
This makes sharing your scripts among collaborators and colleagues simpler, as you don't need to worry about whether or not a differnt user does or does not have the correct package or packages installed.
Use a forward slash to separate a GitHub username and repo name to install an R package from a GitHub repo. For example, to install this package, do:
The same works with Bitbucket, but use a
$ to separate the username from the repo. You may mix and match between CRAN and GitHub packages, like so:
packages("dplyr", "ggplot2", "jakesherman/packages", "Rdatatable/data.table")
Techincally, I've been using the word load incorrectly so far. In R, loading a package means having its contents available in memory, such that you can only access its functions via the
::: operators. Attaching a package means loading it and then adding it to the search path so that you can access its functions directly. You can learn more about the distinctions between loading and attaching here.
By default, packages are attached, just as if you used
base::library. To change this behavior and load a package instead of attaching it, add
:: to the end of a package name in any of the functions, like so:
This becomes powerful as part of the
packages function. A script you write may need one function from a package that you don't want to attach, but want to have installed so that the function can be loaded successfully. Adding
:: to the end of a package name in the
packages function ensures that the package is installed on a different user's machine so that they can run your code.
CRAN, public GitHub and public Bitbucket repos cover many R packages, but not all. In the spirit of making this package flexible, an S3 class called
package_obj has been introduced. You can create a
package_obj by using the
package constructor function.
my_package <- package("packages")
package_obj needs at minimum a name, and by default that name will be installed from CRAN when the
install_package method is called on the object. You may specify an installation function (as well as, optionally, arguments for that function) that will be used to install your function instead of the default. For example, if we want to install a package locally, we could do so like:
local_package <- package("jake_great_package",devtools::install_local,path = "path/to/jake_great.tar.gz")
package_obj objects are accepted as inputs to any of the three major functions in this package:
install_packages (actually, all non-
package_obj objects are converted into
package_objs underneath the hood). For example, we could attach/install the above packages like so:
packages(package("packages"), package("jake_great_package",devtools::install_local,path = "path/to/jake_great.tar.gz"))
You may specify whether
package_obj objects are attached or loaded with the
load_type argument in the constructor function. The default is attach.
You may also choose to import specific functions from a package into the global environment with the
from_import function (inspired by Python). The first argument is the package name, while the next is one or more function names from the package, or a list of function names from the package.
from_import("dplyr", "select", "arrange", "mutate")from_import("dplyr", list("select", "arrange", "mutate")
Thanks for your interest, I hope this package will save you a bit of time going forward. At the moment it is a work-in-progress, so comments and suggestions are appreciated!
R CMD checkfor the first time (#4)