R package installation

How to install an R package on your computer

Author
Published

October 21, 2022

1 What is an R package?

The standard installation of R consists of a set of base, or ‘core’, functions. In addition to those, it is possible to install a large variety of packages with extra functionality.

These packages are developed by the company behind RStudio (Posit), by other companies (e.g. Google, Microsoft, IBM and others), universities, and by R users … perhaps, some day you will write and publish an R package too?

Some of these packages have a very specific and narrow focus – e.g. Steen Harsteds packages for handling motion capture data called mocapr. Other packages er more generic and you will get to know some of them during the course. For instance:

  • tidyverse – a meta-package which makes it easier to work with the R syntax – e.g. by using pipes.
  • ggplot2 – a package for constructing graphs or plots.
  • ggpubr – a package which extends ggplot2, aiming to make it easier to produce publication ready plots.

There are just a few technicalities about R packages, that you need to understand:

1.1 Windows trouble … RTools

To install certain R packages on Windows operating systems, you need to install a program called RTools. You can read more about RTools at this link which includes links to the RTools installer and a more detailed tutorial.

Please note, that this has previously presented problems for those using SDU issued computers, thus check to see if RTools is available through the SDU IT Software Center.

Many R packages are written in, or rely upon, other programming languages such a C and Fortran. Most of the packages, are compiled into a binary package that can run without further ado on Windows. However, if the packages has not been compiled – I will do so itself … and that requires RTools.

1.2 Versions

R packages are specific to the underlying version of R – when a new version of R is published it may take some time for package developers to update their packages. In other words, depending on the packages you make use of, it is not always a good idea to run the absolute latest release of R, but this is mostly an issue if you use ‘niche’ packages.

1.3 Installing and loading packages

If you want to make use of an R package, you need to install and load it before it is available.

  • When you install a package, it is downloaded from the internet and stored on your hard drive.
  • When you load the package, it is read into your computers RAM memory.

Not until the package is in your computers RAM memory will it be available to your code. And thus, any scripts that need extra functionality, also needs to specifically load the packages.

1.4 More than one way to install a package

By far the majority of packages are available online through the Comprehensive R Archive Network – CRAN, but you will not be downloading from the website. Instead, we let R handle the download and installation:

If you want to install a CRAN package you can choose to do so via RStudios graphical user interface or via R in an R console. When installing via the R console, you may get some useful information about the installation process, especially if it fails.

Common misunderstanding: It is not sufficient to have downloaded and installed (install.packages()) to your computers hard drive. You must also, specifically load that package into memory (library() or require()) in the code where it is needed. That way, you can install many packages on your system, but only load the ones you actually need for a project.

…sound in Danish – sorry!

Example installation of an R package