Chapter 7 Libraries and packages

A library or package is a collection of variables, datasets, functions, and/or operators.

It’s called a “package” when being installed install.packages("tidyverse") and a “library” when being loaded for use library(tidyverse).

A library and a package are the same thing, but R people will insist there is a difference. Whenever talking to R people, you’ve got a 50-50 chance of getting it right. If you get it wrong, you’re going to get a short lecture. Just nod, and say “yes, that makes sense, and the distinction is clearly important”. If you say anything else, you’ll get a much longer more boring lecture.

I define these functions, so I don’t have to worry about confusing the two.

install.library = install.packages
package = library

If you only want to access one function or variable in a library without loading the whole thing, you can use ::

dplyr::band_instruments
#> # A tibble: 3 x 2
#>   name  plays 
#>   <chr> <chr> 
#> 1 John  guitar
#> 2 Paul  bass  
#> 3 Keith guitar

7.1 Errors when installing

Like a troll restricting access to a bridge, R will sometimes ask convoluted riddles when installing and especially when updating libraries.

7.1.1 Restart?

One or more of the packages to be updated are currently loaded. Restarting R prior to install is highly recommended.

RStudio can restart R before installing the reuested packages. All work and data will be preserved during restart.

Do you want to restart R prior to install?

[Yes]
[No]
[Cancel] <– No one will blame you if you decide the complexity isn’t worth it

The safer option is [Yes], but you may end up in a ask-restart loop. So try [Yes], but then click [No] if you’re asked a second time.

You’ll have to reload your libraries afterwards by redoing any library(mylibrary) calls you’ve already made.

7.1.2 Locked directory?

Error in install.packages : ERROR: failed to lock directory …
Try removing ‘…/00LOCK’

Unfortunately, R is not compatible with any sort of software that backs up your documents folder. That includes Dropbox, Google Drive, and several others. To work around the problem, either:

  1. Delete the 00Lock directory whenever R complains and retry.
  2. Close or turn off backups while using RStudio.

7.1.3 Sources? Compilation?

Do you want to install from sources the package which needs compilation?

Don’t worry if you’re confused. This convaluted question commonly stumps people. Here is a rephrasing:

We don’t have the latest version of this package for your system. We can (1) try to compile the latest version from source code or (2) install a slightly older version. What would you like to do?

[Yes] = [Try to compile and install new version] This option is slow and can sometimes fail.
[No] = [Install older version] <– You usually want this option.
[Cancel] = [Don't install] Choose this if you don’t think the libray is worth all this effort.