8 Installing packages
8.1 a highly simplified package lifecycle
R Packages covers these phases of the package lifecycle in much more detail.
8.2 Binary packages
- where to get them
- how to know you got them
8.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. 1devtools::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.
8.4 Setting up a development environment
8.4.1 Windows: system prep
On Windows the compiler collection needed for installing packages from source is called Rtools.
Rtools is NOT an R package, so it is not installed with install.packages()
. Instead download it from http://cran.r-project.org/bin/windows/Rtools/ and run the installer.
During the Rtools installation you will see a window asking you to “Select Additional Tasks”.
- Do not select the box for “Add rtools to system PATH”,
devtools
and RStudio should put Rtools on the PATH automatically when it is needed. - Do select the box for “Save version information to registry” (it should be selected by default).
8.4.2 macOS: system prep
On macOS you will need to install the Xcode Command Line Tools, which may already be installed. You can check if they are by running
::has_devel() devtools
If they are not installed you have a few options.
Minimalist approach (what I do): Install Xcode Command Line Tools. In the shell:
xcode-select --install
Install the current release of full Xcode from the Mac App Store. WAY more stuff than you need but the advantage is App Store convenience.
8.4.2.1 What about Homebrew?
Users on macOS often install R with homebrew via this formula:
brew install r
Unfortunately, when R is installed in this way it is not compatible with the CRAN package binaries, which means you must build and install all packages from source. This takes additional time during installation and can lead to more time spent dealing with installation issues if a package fails to compile.
Instead, if you prefer the convenience of homebrew, we recommend installing the r
cask. NOTE: the cask used to be named r-app
but the -app
suffix was dropped due to a homebrew policy change around 2019-03-11.
brew install --cask r
This will install the CRAN R distribution, so all package binaries will be available just like they would be from installing R manually.
8.4.3 Linux system prep
Most Linux systems will typically be equipped with the necessary tools for building packages from source. The system package manager (e.g. apt
, yum
, zypper
, etc.) can be used to install tools if they are missing.
8.4.4 Verify system prep
::has_devel() devtools
If this function runs without error then congratulations, your R installation is properly set up!
8.4.5 What about Conda?
Some users use conda in python contexts and notice that conda now also provides (some) R package binaries.
However we would suggest avoiding conda at this time, only a limited number of all CRAN packages are available 2 and many users run into installation problems trying to use install.packages()
inside conda environments. Using install.packages()
also means you no longer declare all dependencies in the same location. Which means your work is less reproducible than if you always install only conda packages.
For these reasons we suggest you either restrict yourself only to packages available as official conda packages, or avoid using conda for R.
8.5 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.
library(devtools)
<- "~/tmp/tmp_library"
tmp_lib dir.create(tmp_lib)
::install_github("dill/beyonce", lib = tmp_lib)
devtools
## 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.