Here, we will discuss packages, libraries, and getting help in R; and how to install, load, and uninstall packages in R.

1 What Are Packages in R?

Packages are a set of functions and data sets that add specific functionality to R on top of the base version. Users can install packages of interest to perform specific tasks or access specific data.

When a package is installed, it will be downloaded to an R library folder on the computer and can later be accessed by R.

The base version of R has several packages that are automatically loaded whenever an R session is started which include:

Summary of Functions for Installing and Loading Packages, and Getting Help in R
Function Usage
install.packages("package") Install a package
library(package) Load a package
require(package) Load a package
remove.packages("package") Uninstall a package
.libPaths() Find R libary folder(s)
library() Check installed packages
installed.packages() Check installed packages
search() Check loaded packages
sessionInfo() Check loaded packages
searchpaths() Find loaded packages folders
library(help = "package") Check functions or data in a package
?package Get help with a package
?function Get help with a function
?dataset Get help with a dataset

2 Installing, Loading and Uninstalling or Removing Packages in R

Install a Package

To install a package, you can use the "install.packages" function from "utils":

install.packages("package")

For example:

install.packages("ggplot2")

Load a Package

After installation, to load the package to your R session use the library() or require() function from "base":

library(package) # No need for quotation marks.
require(package) # No need for quotation marks.

For example:

library(ggplot2)

The difference between library() and require() is that library() will immediately lead to an error if the called package is not installed while the session run continues with require() but can fail later.

Uninstall or Remove a Package

To uninstall or remove a package, you use the remove.packages() function from "utils":

remove.packages("package")

For example:

remove.packages("ggplot2")

3 Checking Installed or Loaded Packages in R

Check Installed Packages

To check for the installed packages in R, use the library() function from "base" without specifying a package:

library()

Or to list them within the console, use the installed.packages() function from "utils":

installed.packages()

Check Loaded Packages

To check for the loaded packages, use the search() function from "base" function without specifying an argument:

search()

Or for additional information, use the sessionsInfo() function from "utils":

sessionInfo()

Get Location of Loaded Packages

To find your R library folder(s) where packages are stored on your computer, use the .libPaths() function from "base" without specifying an argument:

.libPaths()

To see the location of the loaded R packages on your computer, use the searchpaths() function from "base" without specifying an argument:

searchpaths()

Get List of Functions or Data in a Package

For a complete list of functions or data in an R package, use library(help = "package").

For example:

library(help = "utils")

4 Getting Help with Packages, Functions and Datasets in R

You can get help with packages, functions and data sets in R. This will provide you with background information on what a package entails, tasks a function can perform, and content of a data set in a web browser or the RStudio Help browser in RStudio.

Get Help with a Package

To get help with a package’s usage:

?package

For example:

?base

Get Help with a Function

To get help with a function’s usage:

?function

For example:

?mean

Get Help with a Dataset

To get help with a dataset’s usage:

?dataset

For example:

?mtcars

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes