9  Installing packages

9.1 a highly simplified package lifecycle

%%{init: {"theme": "dark" } }%%
graph TD
Source -- "devtools::build()" --> Bundled
Bundled -- "devtools::build(binary = TRUE)"--> Binary 
Binary --"install.packages()"--> Installed 
Installed --"library()"--> Loaded 
%%{init: {"theme": "dark" } }%%
flowchart TD
subgraph dev
  direction LR
Source -- "devtools::build()" --> Bundled
Bundled -- "devtools::build(binary = TRUE)"--> Binary 
subgraph use
direction LR
Binary --"install.packages()"--> Installed 
Installed --"library()"--> Loaded 

R Packages covers these phases of the package lifecycle in much more detail.

9.2 Binary packages

  • where to get them
  • how to know you got them

9.3 Source packages

The most common type of package you install is a binary package. Packages released on CRAN are built as pre-compiled binaries.

However often it is useful to install packages which do not have a pre-built binary version available. This allows you to install development versions not yet released on CRAN, as well as older versions of released packages. It also lets you build your own packages locally.

To install a source package you will need to setup a development environment.

There are a few main functions used to install source packages.

  • devtools::install_dev() to install the latest development version of a CRAN package. 1
  • devtools::install_github() to install a package directly from GitHub, even if it is not on CRAN.
  • devtools::install_version() to install previously released CRAN versions of a package.

For example devtools::install_dev("dplyr") will install the development version of dplyr. devtools::install_github("jimhester/lookup") will install Jim’s lookup package (which is not on CRAN), and devtools::install_version("readr", "1.0.0") will install readr 1.0.0.

It is also possible to fork, clone and work with a package directly then use devtools::install() and devtools::load_all() to work with the package locally like you would with a package you have created yourself.

9.4 Installation to a temporary library

It is sometimes useful to install packages to a temporary library, so that they don’t affect your normal packages. This can be done by using the lib argument to the devtools install functions, then using lib.loc in library() when you load the package.


tmp_lib <- "~/tmp/tmp_library"

devtools::install_github("dill/beyonce", lib = tmp_lib)

## restart R

## explicitly load the affected packages from the temporary library
library(beyonce, lib.loc = tmp_lib)

## your experimentation goes here

## done? clean up!
unlink(tmp_lib, recursive = TRUE)

Try the activity: usethis::use_course("rstd.io/wtf-source-package")

To practice installing various types of source packages.

  1. This will only work if the package includes a link to the development location in the package DESCRIPTION↩︎