⇦ Back

1 Synopsis

  • A ‘package’ is the same as a ‘library’
  • Installing packages increases the number of functions you can call
  • Once installed, you still need to import the library into your script

2 What are Packages and Libraries, and Why Do I Need Them?

When you download and install R or RStudio you get access to what is known as base R. It’s very powerful and can do a lot of things but, as the name suggests, it’s very basic. In order to add functionality and be able to use other functions you need to install packages and import libraries.

What’s the difference between a library and a package?
Nothing. They are two names for the same thing. For some reason you install a package from the internet and, once it has been installed to your local machine, you import it as a library into R. I don’t know why the act of installing it changes its name.

3 Install a Package

Packages can be installed by running the install.packages() function in R. Specify the name of the package you want to install as a string and the repository you want to install it from by using the repos keyword argument:

install.packages("BlandAltmanLeh", repos = "http://cran.us.r-project.org")
install.packages("chron", repos = "http://cran.us.r-project.org")
install.packages("DiagrammeR", repos = "http://cran.us.r-project.org")
install.packages("dplyr", repos = "http://cran.us.r-project.org")
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
install.packages("ggpubr", repos = "http://cran.us.r-project.org")
install.packages("kableExtra", repos = "http://cran.us.r-project.org")
install.packages("knitr", repos = "http://cran.us.r-project.org")
install.packages("lintr", repos = "http://cran.us.r-project.org")
install.packages("logging", repos = "http://cran.us.r-project.org")
install.packages("maps", repos = "http://cran.us.r-project.org")
install.packages("matrixStats", repos = "http://cran.us.r-project.org")
install.packages("Rmisc", repos = "http://cran.us.r-project.org")
install.packages("R.utils", repos = "http://cran.us.r-project.org")
install.packages("readr", repos = "http://cran.us.r-project.org")
install.packages("reticulate", repos = "http://cran.us.r-project.org")
install.packages("rmarkdown", repos = "http://cran.us.r-project.org")
install.packages("stringi", repos = "http://cran.us.r-project.org")
install.packages("stringr", repos = "http://cran.us.r-project.org")
install.packages("titanic", repos = "http://cran.us.r-project.org")
install.packages("utf8", repos = "http://cran.us.r-project.org")

The “cran” part of the repos URL refers to “CRAN”, the Comprehensive R Archive Network where most R packages are stored online.

You only need to install a package once on your computer. After it’s finished installing you can delete the install.packages() functions from your R script.

You can find out where on your computer the packages are installed by running .libPaths() and see what packages you’ve installed with print(as.data.frame(installed.packages())).

3.1 Install a Version of a Package Other Than the Latest One

If there is a specific version of a package you want to install, find it on the CRAN archive (https://cran.r-project.org/src/contrib/Archive/) then download it as follows:

# Install vctrs version 0.3.8
packageurl <- "https://cran.r-project.org/src/contrib/Archive/vctrs/vctrs_0.3.8.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

3.2 Install a Package for the First Time in Ubuntu

If you are on an Ubuntu machine and you get an error message along the lines of:

Warning in install.packages :
  'lib = "/usr/local/lib/R/site-library"' is not writable`

It is because the default folder location where your packages get installed (/usr/local/lib/R/site-library) is not writable on your computer. Fix this by creating a personal library and downloading packages to that instead. Helpfully, R will offer to do this for you if you try to install a package from the terminal:

  • Open your terminal
  • Open R by running R
  • Run install.packages(<name>) where <name> is the name of a package you want to install
  • You should see the message Would you like to use a personal library instead? (y/n)
  • Respond with y to this and the next message. From now on you should be able to install packages normally (both in an IDE and in the terminal).

Some other programmes might be needed by Debian in order for certain packages to work. Install them from the terminal with:

sudo apt install libssl-dev
sudo apt install libxml2-dev
sudo apt install libcurl4-openssl-dev
sudo apt install libcairo2-dev

4 Updating R

Sometimes you can only download a package if it’s compatible with the version of R you have installed. In general, you always want to work with the latest version of R and latest versions of packages. If you’re on a macOS or Linux machine (eg Ubuntu) you can check what version of R you have installed from the terminal with:

R --version

To update R manually, download and install the latest version from the R website. When you re-start RStudio it should automatically use this latest version. Note that if you are moving to a new major version (eg from version 3.5 to version 4.0) then you will have to re-install all your packages. Trying to simply update them will not work, you actually need to re-install them. The normal methods of package installation should work fine so try them first, but if you are having issues the easiest thing is to delete all of your package files completely and then re-install from scratch. To find where your package files are stored on your computer run .libPaths() in R and open the folder that is returned. Delete all the folders in there.

On Ubuntu the folder is usually /usr/local/lib/R/site-library
On macOS the folder is usually /Library/Frameworks/R.framework/Versions/<version>/Resources/library

If you are using Ubuntu, you have the option of updating R from the terminal (there is a package called updateR that allows you to update R from the terminal on macOS but it doesn’t always work). Instructions on how to do this are below. Note that you will still need to re-install your packages if you are moving to a new major version number.

4.1 Updating R from the Terminal on Ubuntu

If you are using Ubuntu and aren’t using RStudio, update R by first opening the /etc/apt/sources.list file for editing with sudo permission, for example:

sudo nano /etc/apt/sources.list

Then add whichever of the following lines corresponds to your Ubuntu version:

# Ubuntu 22.04 (Jammy Jellyfish)
deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/
# Ubuntu 20.04 (Focal Fossa)
deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/
# Ubuntu 18.04 (Bionic Beaver)
deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/
# Ubuntu 16.04 (Xenial Xerus)
deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran40/

The above are relevant for R version 4. Change the -cran40/ part to whatever version number you are interested in. Then run the following:

sudo apt-get update
sudo apt-get install r-base r-base-dev

To update any R libraries installed via APT, you might need to run sudo apt-get upgrade again.

If the above doesn’t work and gives you errors similar to the following:

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ InRelease: The following signatures were invalid: EXPKEYSIG 51716619E084DAB9 Michael Rutter <marutter@gmail.com>
W: Failed to fetch https://cloud.r-project.org/bin/linux/ubuntu/focal-cran40/InRelease  The following signatures were invalid: EXPKEYSIG 51716619E084DAB9 Michael Rutter <marutter@gmail.com>
W: Some index files failed to download. They have been ignored, or old ones used instead.

…it is because you need to add the signing key for the R binaries. See here for how to do that.

5 Import a Library

When you want to use functionality that is part of a particular package you have installed you need to import that package as a library into your script by using the library() function:

# Import the 'dplyr' library in order to be able to use its functions
library(dplyr)
## 
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
## 
##     filter, lag
##
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Some packages print text to the console when they are loaded, and dplyr is one of them (as you can see above). If you find this annoying you can turn the feature off by setting warn.conflicts to FALSE:

# Import the 'dplyr' library without generating text
library(dplyr, warn.conflicts = FALSE)

You can now use the functionality that is included with the library you imported:

# As an example, you can now use the 'pipe' operator which puts a value into
# the front of the next pair of brackets:
x <- 49
square_root <- x %>% sqrt()
# This will be the square root of 49:
square_root
## [1] 7

Note: best practice is to import all of the libraries you need in a script right in the beginning, in alphabetical order:

# Start of your script
library(dplyr)
library(ggplot2)
library(logging)

# The rest of your code follows from here...

If you get an error message like the following:

Error in loadNamespace(x) : there is no package called ‘dplyr’

It means you do not have that package - in this case the dplyr package - installed (or it has not installed correctly). Use the install.packages() function again.

6 Call a Function From a Library

You can call a function that comes from a particular library in one of two ways. Here’s an example with the str_length() function, which comes from the ‘stringr’ library and finds the number of characters in a string. The first way is to just call the function as you would any other:

# Import the library
library(stringr)
# Find the length of a string of text
length <- str_length("Hello, World")
# Output to the console
print(length)
## [1] 12

While this is the simplest way of doing it, it can sometimes get confusing as to which function came from which library. This is especially true if you have imported a lot of them and are using all sorts of different functions all over the place. Consider using the alternative method, which involves using two colons to explicitly show which library the function came from:

# Find the length of a string of text
length <- stringr::str_length("Hello, World")
# Output to the console
print(length)
## [1] 12

7 Alternative Methods of Importing Libraries

If your script is going to be run on someone else’s computer and you don’t know if they will have a particular package installed, import it into your script with the require() function. If the package is installed, it will import it. If it isn’t, it will first download and install it, then import it.

require(ggplot2)
require(logging)

Sometimes, the messages that get printed to the console when a package is installed or imported get annoying. Some (as mentioned above with regards to dplyr) give you the option to turn these off by, for example, setting warn.conflicts to FALSE. Other packages don’t give you this option, but in those cases you can suppress messages by using suppressPackageStartupMessages():

suppressPackageStartupMessages(require(ggplot2))
suppressPackageStartupMessages(library(logging))

8 Summary

  • A ‘package’ is the same as a ‘library’
  • Increase the number of functions you can call by installing packages
  • Once installed, you still need to import the library into your script

⇦ Back