2.3 Packages Management

2.3.1 Load packages

Q: What is the difference btw library(package) and require(package)?
A:

  • library(package) returns an error if the package doesn’t exist.

  • require(package) returns FALSE if the package is not found and TRUE if the packages is loaded. require is designed for use inside other functions, such as using the value it returns in some error checking loop, as it outputs a warning and continues if the package is not found.

Q: How to reload a package after updating?
A: Call detach(package:pkg, unload = TRUE) or unloadNamespace first, then use library(pkg) to reload. If you use library on a package whose namespace is loaded, it attaches the exports of the already loaded namespace. So detaching and re-attaching a package may not refresh some or all components of the package, and is inadvisable. The most reliable way to completely detach a package is to restart R.

For example, if we want to detach ggplot2 package, we can use

detach(package:ggplot2, unload=TRUE)

requireNamespace can be used to test if a package is installed and loadable because it comes back with either TRUE (if found the pkg) or FALSE (if failed to find the pkg).

> !requireNamespace("ggplot2")
[1] FALSE
> !requireNamespace("ggplot3")
Loading required namespace: ggplot3
Failed with error:  ‘there is no package called ‘ggplot3’’
[1] TRUE

To see whether need to install some packages:

# install the package if it is not available
if (!requireNamespace("devtools")) install.packages("devtools")
# or equivalently
if (!require("devtools")) install.packages("devtools")

You can also use require(devtools) to check whether the required package is available, but note that it will load the package as a side effect.

Alternatively,

# short command
"ggplot2" %in% installed.packages()
# full command
"ggplot2" %in% rownames(installed.packages())

installed.packages() Finds details of all packages installed in the specified library path lib.loc. Returns a matrix of package names, library paths and version numbers.

> installed.packages() %>% class()
[1] "matrix" "array" 

> installed.packages() %>% str()
 chr [1:355, 1:16] "abind" "alphavantager" "anytime" "askpass" "assertthat" "backports" "base" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:355] "abind" "alphavantager" "anytime" "askpass" ...
  ..$ : chr [1:16] "Package" "LibPath" "Version" "Priority" ...

The following code can be used to load packages for your project and set up the working environment.

# load the pkg, if not found, install then load
require(dplyr) || {install.packages("dplyr"); require(dplyr)}
require(odbc) || {install.packages("odbc"); require(odbc)}
require(DBI) || {install.packages("DBI"); require(DBI)}

If using library(), will return error if some package is not installed and interrupt the program.

If it is a list of packages you want to check, use lapply to loop through all packages.

## First specify the packages of interest
packages = c("MASS", "nlme")

## Now load or install&load all
package.check <- lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)

You can then use search() to determine whether all the packages have loaded.

search()
 [1] ".GlobalEnv"        "package:nlme"      "package:MASS"     
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:datasets"  "renv:shims"        "package:utils"    
[10] "package:methods"   "Autoloads"         "package:base"     

Q: dplyr has many conflicts with plyr.
A: Specify pkg using ::. Or set library priority by

  • changing the order in which you load the packages.
# load dplyr last so that it has priority
library(plyr)
library(dplyr)
  • with the {needs} package
library(needs)
# prioritize the functions in dplyr
prioritize(dplyr) 

Q: How to unload a package without restarting R?
A: detach("package:ggplot2", unload=TRUE) or uncheck the checkbox button in Packages pane.


Q: How to remove a package?
A: Use remove.packages("dplyr") or you can use the package manager pane, click the X mark on the right side of the selected package.


2.3.2 Install packages

Install R packages from source

# From local tarball
install.packages(
  # indicate path of the package source file
  "~/Documents/R/UserPackages/shoRtcut2_0.1.0.tar.gz", 
  # indicate it is a local file
  repos = NULL)

# From github
install.packages("Rcpp", repos="https://rcppcore.github.io/drat")

Install from GitHub

devtools::install_github(repo, ref="HEAD", subdir = NULL)
  • repo repository address in the format username/repo[/subdir][@ref|#pull]. Alternatively, you can specify subdir and/or ref using the respective parameters. If both are specified, the values in repo take precedence.
  • ref Desired git reference. Could be a commit, tag, or branch name, or a call to github_pull() or github_release(). Defaults to "HEAD", which means the default branch on GitHub and for git remotes.

Ex

# install version 3.5.1
install_github("tidyverse/ggplot2", ref="ggplot2 3.5.1")

Check installed packages

# print all installed packages
rownames(installed.packages())
# check if `ggplot2` is installed
"ggplot2" %in% rownames(installed.packages())

installed.packages(lib.loc=NULL, priority=NULL)

  • lib.loc character vector describing the location of R library trees to search through
  • priority used to select packages; "high" is equivalent to c("base", "recommended")
# list all bases packages from your `R.Version`
> rownames(installed.packages(priority="base"))
 [1] "base"      "compiler"  "datasets"  "graphics"  "grDevices" "grid"     
 [7] "methods"   "parallel"  "splines"   "stats"     "stats4"    "tcltk"    
[13] "tools"     "utils" 
#  what R loads on startup
> c(getOption("defaultPackages"), "base")
[1] "datasets"  "utils"     "grDevices" "graphics"  "stats"     "methods"   "base"     

getOption("defaultPackages") is what R loads on startup although the basepackage is not counted.

Check package version

packageVersion("ggplot2") # check package version

Q: How do I know if I have the latest version?
A: You can go to GitHub repo to check release notes. You will find the latest version of packages there.


2.3.3 Update packages

  • Update an individual package

    • Using install.packages

      install.packages("ggplot2") # update one specific package
    • Using update.packages

      update.packages(oldPkgs = "ggplot2")

      Note that you need to specify oldPkgs explicily as it is a named argument.

  • Update ALL outdated packages

    ## update all installed packages in a stated library location, default to `.libPaths()`
    update.packages(lib.loc = .libPaths(), ask = TRUE) 

    update.packages updates ALL outdated packages in a stated library location. That library location is given by the first argument (if not supplied it works on all known library locations for the current R session).
    It will ask you for every package if you want to update.
    To just say yes to everything, use ask = FAlSE.

    update.packages(ask = FALSE)

    Unfortunately this won’t update packages installed by devtools::install_github()


Troubleshooting

Q: I ran update.packages("ggplot2"), but nothing happened. No output on console, no error, nothing.
A: The first argument specifies the library location you want to search through (and update packages therein). update.packages("ggplot2") means you want to update the packages in library location ggplot2, which is most unlikely to exist on your R installation.


Q: I tried to update ggplot2 with install.packages("ggplot2"), but nothing happened.
A: If ggplot2 is already loaded, then you can’t install ggplot2 in the current session now. If you need to, save any objects you can’t easily recreate, and quit out of R. Then start a new R session, immediately run install.packages("ggplot2"), then once finished, load the package and reload in any previously saved objects.


More about update.packages:

  • update.packages(lib.loc = NULL, repos = getOption("repos"), ask = TRUE): First a list of all packages found in lib.loc is created and compared with those available at the repositories. If ask = TRUE (the default) packages with a newer version are reported and for each one the user can specify if it should be updated. If so the packages are downloaded from the repositories and installed in the respective library path (or instlib if specified).

  • You can specify one specific package to update using update.packages(oldPkgs = "ggplot2"). It will check updates only for that package and ask you if you want to update.

    The easiest way to update an individual package is just to use install.packages. It is a one step command, compared to update.packages, which first checks and then asks.

  • update.packages returns NULL invisibly.

  • Be aware that some package updates may cause your previous code to stop working. For this reason, we recommend updating all your packages once at the beginning of each academic year (or semester) – don’t do it before an assessment or deadline just in case!


Reinstall all Packages after R update

R packages are missing after updating. So you have to save the installed packages and re-install them after updating.

Here is how to do it manually.

## get packages installed
packs <- as.data.frame(installed.packages(.libPaths()[1]), stringsAsFactors = F)
# Save to local
f_name <- "~/Documents/R/packages.csv"
rownames(packs)
write.csv(packs, f_name, row.names = FALSE)
packs <- read_csv(f_name)
packs
## Re-install packages using install.packages() after updating R
install.packages(packs$Package)

R library path /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

  • use find.package("ggplot2") to find the location to where the given package is found.
  • alternatively, you can run .libPaths()
    • .libPaths() without an argument will return a list of all the places R will currently look for loading a package when requested.
    • .libPaths("a/b/c") with an argument will add that new directory ("a/b/c") to the ones R was already using. If you use that directory often enough, you may wish to add that call to .libPaths("a/b/c") in your .Rprofile startup file in your home directory.

2.3.4 Put your R package on GitHub

Reference: https://jennybc.github.io/2014-05-12-ubc/ubc-r/session2.4_github.html

  • Change to the package directory

  • Initialize the repository with git init

  • Add and commit everything with

    1. git add . stage changes;
    2. git status optional check staged changes, but yet to submit;
    3. and git commit submit staged changes.
  • Create a new repository on GitHub

  • Connect your local repository to the GitHub one

    # add repo name "origin" to the remote repo at the URL
    git remote add origin https://github.com/username/reponame
  • Push everything to github

    # rename the current local branch to "main"
    git branch -M main
    # creates a remote branch "origin" and sets it upstream of the "main" branch
    git push -u origin main

FAQ

Q: What are package vignettes?
A: It’s important to write good and clear documentation, but users don’t often read it; at best they’ll look at the examples, so be sure to include informative examples. In my experience, what users really want are instructive tutorials demonstrating practical uses of the software with discussion of the interpretation of the results. In R packages, such tutorials are called “vignettes.”