Here, we will discuss packages, libraries, and getting help in R; and how to install, load, and uninstall 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:
"base": base R functions such as Sys.time()
,
log()
, and sort()
for mathematical operations and data handling.
"stats": functions for statistical calculations
such as lm()
,
t.test()
,
and rnorm()
for regression, tests, and distributions & random
sampling.
"graphics": functions for base graphics such as
text()
and hist()
or other plots &
charts.
"grDevices": functions which support both base
and grid graphics such as pdf()
and
savePlot()
.
"utils": collection of utility functions such as
help()
and install.packages()
.
"datasets": variety of datasets such as
"iris"
and "mtcars"
.
"methods": formally defined methods and classes
for R objects such setClass()
and
setMethod()
.
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 |
To install a package, you can use the "install.packages"
function from "utils"
:
For example:
After installation, to load the package to your R session use the
library()
or require()
function from
"base"
:
For example:
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.
To uninstall or remove a package, you use the
remove.packages()
function from "utils"
:
For example:
To check for the installed packages in R, use the
library()
function from "base"
without
specifying a package:
Or to list them within the console, use the
installed.packages()
function from
"utils"
:
To check for the loaded packages, use the search()
function from "base"
function without specifying an
argument:
Or for additional information, use the sessionsInfo()
function from "utils"
:
To find your R library folder(s) where packages are stored on your
computer, use the .libPaths()
function from
"base"
without specifying an argument:
To see the location of the loaded R packages on your computer, use
the searchpaths()
function from "base"
without
specifying an argument:
For a complete list of functions or data in an R package, use
library(help = "package")
.
For example:
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.
To get help with a package’s usage:
For example:
To get help with a function’s usage:
For example:
To get help with a dataset’s usage:
For example:
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes