R Packages
A package is a collection of R functions, example data, and compiled code, including R programs, documentation, examples, test data, and so on.
R-related packages are generally stored in the "library" directory under the installation directory. By default, some common packages come with R after installation; of course, we can also add packages we need later on.
For a complete list of R packages, refer to:https://cran.r-project.org/web/packages/available_packages_by_name.html
Next, we mainly introduce how to install R packages.
View 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"
View installed packages
We can use the following function to view the 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
View the loaded packages
We can use the following function to view the 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 a new package
A new package can be installed using install.packages() function, with the following format:
install.packages("要安装的包名")
We can directly set the package name and get it from CRAN the website to obtain the package; in the following example we load the XML package:
# 安装 XML 包
install.packages("XML")
Or we can directly download the relevant package from CRAN and install it locally:
install.packages("./XML_3.98-1.3.zip")
In China, it is generally recommended to use a domestic mirror; the following example uses the University of Science and Technology of China source for installation:
# 安装 XML 包
install.packages("XML", repos = "https://mirrors.ustc.edu.cn/CRAN/")
One of the configuration files for a CRAN (The Comprehensive R Archive Network) mirror is .Rprofile (located at ~/.Rprofile on Linux).
Add the following lines at the end of the file:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
Once you open R, you can use this CRAN mirror to install R packages.
Using a package
A newly installed package must first be loaded into the R environment before it can be used; the format is as follows:
library("包名")
The following example loads the XML package:
library("XML")