Easy Tutorial
❮ R Bar Charts R Input Json File ❯

R Packages

A package in R is a collection of functions, example data, and precompiled code, including R programs, documentation, examples, and test data.

R language packages are typically stored in the "library" directory under the installation directory. By default, some commonly used packages are included with the R language installation, but we can also add custom packages later.

A complete list of R language packages can be found at: https://cran.r-project.org/web/packages/available_packages_by_name.html

Next, we will mainly introduce how to install R language packages.

Viewing the Installation Directory of R Packages

We can use the following function to view the installation directory of R packages:

Example

> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"
>

Viewing Installed Packages

We can use the following function to view installed packages:

library()

The output is as follows:

base                    The R Base Package
boot                    Bootstrap Functions (Originally by Angelo Canty for S)
class                   Functions for Classification
cluster                 "Finding Groups in Data": Cluster Analysis Extended Rousseeuw et al.
codetools               Code Analysis Tools for R
compiler                The R Compiler Package
datasets                The R Datasets Package
foreign                 Read Data Stored by 'Minitab', 'S', 'SAS', 'SPSS', 'Stata', 'Systat', 'Weka', 'dBase', ...
graphics                The R Graphics Package
grDevices               The R Graphics Devices and Support for Colours and Fonts
grid                    The Grid Graphics Package
KernSmooth              Functions for Kernel Smoothing Supporting Wand & Jones (1995)
lattice                 Trellis Graphics for R
MASS                    Support Functions and Datasets for Venables and Ripley's MASS

Viewing Loaded Packages

We can use the following function to view packages loaded in the environment:

Example

> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"

Installing New Packages

New packages can be installed using the install.packages() function, as follows:

install.packages("package_name_to_install")

We can directly specify the package name to fetch from the CRAN website. For example, we load the XML package:

# Install XML package
install.packages("XML")
install.packages("XML")

Alternatively, we can download the relevant package directly from CRAN and install it locally:

install.packages("./XML_3.98-1.3.zip")

It is generally recommended in China to use a domestic mirror. The following example uses the Tsinghua mirror for installation:

# Install the XML package
install.packages("XML", repos = "https://mirrors.ustc.edu.cn/CRAN/")

CRAN (The Comprehensive R Archive Network) mirror source configuration files include .Rprofile (located at ~/.Rprofile on Linux).

Add the following statement at the end of the file:

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))

Open R to use this CRAN mirror source to install R packages.

Using the Package

Newly installed packages need to be loaded into the R environment before they can be used, in the following format:

library("package_name")

The following example loads the XML package:

library("XML")
❮ R Bar Charts R Input Json File ❯