Packages

Author

Marie-Hélène Burle

Packages are a set of functions, constants, and/or data developed by the community that add functionality to R.

In this section, we look at where to find packages and how to install them.

Looking for packages

Managing R packages

For this course, you won’t have to install any package as they have already been installed in our RStudio server.

R packages can be installed, updated, and removed from within R:

install.packages("<package_name>", repos="<url-cran-mirror>")
remove.packages("<package-name>")
update_packages()

Example:

install.packages("rvest", repos="https://mirror.rcg.sfu.ca/mirror/CRAN/")

The first time you install a package, R will ask you whether you want to create a personal library in your home directory. Answer yes to both questions. Your packages will now install under ~/.

Some packages require additional modules to be loaded before they can be installed. Other packages need additional R packages as dependencies. In either case, you will get explicit error messages. Adding the argument dependencies = T helps in the second case, but you will still have to add packages manually from time to time.

Loading packages

To make a package available in an R session, you load it with the library() function.

Example:

library(readxl)

Alternatively, you can access a function from a package without loading it with the syntax: package::function().

Example:

readxl::read_excel("file.xlsx")

Package documentation

  • Select a package from the list of CRAN packages.
  • Google “cran” and the name of your package (e.g. “cran dplyr”).
  • Look up a package in the package documentation.
  • Get a list of functions within a package with the help() function (installed, but not loaded in session):

Example to get a list of functions in the dplyr package:

help(package = "dplyr")
  • Get help on a function within a package:

If you are using RStudio or the HTML format for your R help and you already ran the command to get the list of functions within a package (e.g. help(package = "dplyr")), you can get help on any function by clicking on its name.

If you are using the text format for help (for instance, if you are running R remotely on the command line), you can get help for any function by adding its name at as the first argument of the previous command.

Example to get help on the function bind() of the package dplyr:

help(bind, package = "dplyr")

Of course, if the dplyr package is already loaded in your session, you can simply run help(bind).

  • Get a list of all help files with alias or concept or title matching a regular expression in all installed packages:

Example to get a list of all help files with alias or concept or title matching bind:

??bind

You can then open those help files as seen previously.

  • Get a list of all vignettes for all installed packages:

If you are using RStudio or the HTML help format:

browseVignettes()

If you are using the text help format:

vignette()
  • Get a list of vignettes available for a package (not all packages have vignettes):

Example to get a list of vignettes for the package dplyr:

If you are using RStudio or the HTML help format:

vignette(package = "dplyr")

If you are using the text help format:

browseVignettes(package = "dplyr")

You can then open those help vignettes as seen previously.