R for modellers - Vignette 03

Installing and loading packages

Julien Arino

Department of Mathematics

University of Manitoba*




* The University of Manitoba campuses are located on original lands of Anishinaabeg, Cree, Oji-Cree, Dakota and Dene peoples, and on the homeland of the Métis Nation.

Required for MATH 2740 students!


  • If you are a student in the University of Manitoba’s Mathematics of Data Science (MATH 2740) course, you must read the “friendly” method


  • In particular, failure to use this method may result in loss of marks in your R assignments!

Packages (a.k.a. libraries)

Why use packages?


  • Packages (or libraries) extend R by providing functions or data that are useful in particular contexts


  • Packages allow to avoid “bloating”, since the R core remains relatively light and you only install the additional content you need

CRAN is the main source for packages


paste(nrow(available.packages()),
      "packages on CRAN on",
      Sys.Date())
[1] "21695 packages on CRAN on 2024-11-25"

There are also packages not on CRAN

  • For instance, packages on GitHub


  • Sometimes packages get removed from CRAN, although the latest versions are typically still available


  • Installation is a little different (detailed later)

Installing a package

From CRAN

  • Use the command install.packages()
install.packages("deSolve")



  • You can also use RStudio

From GitHub

  • Some packages are only on GitHub; others have their latest (testing) version on GitHub and a stable version on CRAN


  • Need to use the devtools or remotes packages (maybe prefer devtools)


Example: xkcd style graphs

> library(githubinstall)
> githubinstall("xkcd")
Select a number or, hit 0 to cancel. 

1: DanHenebery/Rxkcd  
2: EDiLD/xkcd2        

Selection: 2
Suggestion:
 - EDiLD/xkcd2  NA
Do you want to install the package (Y/n)?  
Downloading GitHub repo EDiLD/xkcd2@master

Installing a removed package



  • Actually can be found on GitHub (here and a fork), so could use previous method, but to illustrate…


  • Grab the newest archived version from CRAN


install.packages('~/Downloads/htmltab_0.8.2.tar.gz', 
                 type='source')


or we can point directly to the link


url = "https://cran.r-project.org/src/contrib/Archive/htmltab/htmltab_0.8.2.tar.gz"
install.packages(url, 
                 type='source')

Loading a package

Loading a package

  • Once a package is installed, you load it with the command library()


  • No need to use quotation marks: library(ggplot2)


  • You can also use RStudio

Loading several packages


To load several packages with a single command, you need to use a loop and the option character.only = TRUE


required_packages = c("ggplot2", "dplyr")
for (p in required_packages) {
  library(p, character.only = TRUE)
}

Be friendly to others

When distributing your code, think of those using it

  • If you use a slightly unusual library, it is possible that a person you share your code with does not have that library installed

    • It is nice to them if you spare them having to do the work to install the library


  • But it is also possible that they already have the library

    • It will be annoying to them if you trigger an installation of the library (especially under linux where libraries are compiled for installation)

So test whether the library is installed

  • If it is, load it
  • If it is not, install it then load it
if (!require(package_name)) {
    install.packages("package_name")
    library(package_name)
}

require is designed for use inside other functions; it returns FALSE and gives a warning (rather than an error as library() does by default) if the package does not exist.

Example using several libraries


required_packages = c("ggplot2", "dplyr")
for (p in required_packages) {
  if (!require(p, character.only = TRUE)) {
    install.packages(p)
    library(p, character.only = TRUE)
  }
}

Updating packages

Using RStudio


  • RStudio has a menu to check if updates are available to libraries and update them if that is the case

From the R command line

update.packages(ask = FALSE, checkBuilt = TRUE, 
                Ncpus = 6)
  • ask = FALSE: do not ask for confirmation
  • checkBuilt = TRUE: check with which version of R the package was built and call package old if this is a earlier major version (e.g., built with 4.2 when the current is 4.3)
  • Ncpus = 6: run compilations (if needed) using that many threads

Packages and R major versions

  • Packages are stored by default in your home folder under the current major version of R (currently 4.3)
~/R/x86_64-pc-linux-gnu-library/4.3/
  • When the major version changes, you therefore need to do something with all your current packages..

  • There is no planned mechanisms for doing this!

  • Easiest: export list of libraries in the previous version, then install all of them

Set lib to be previous R version (the one prior to the update), say, 4.2:

lib = 
  "/home/jarino/x86_64-pc-linux-gnu-library/4.2"


then get list of packages

installed_packages = 
  as.data.frame(installed.packages(lib))$Package


then use the method described earlier to install all these packages