R Packages
A package is a collection of R functions, example data, and precompiled code, including R programs, documentation, examples, test data, etc.
R language packages are generally stored in the "library" directory under the installation directory. By default, after R is installed, it comes with some commonly used packages. Of course, we can also customize and add some packages to use later.
The 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.
View R package installation directory
We can use the following function to view the installation directory of R packages:
Example
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"
>
View installed packages
We can use the following function to view installed packages:
library()
The output result 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 loaded packages
We can use the following function to view the packages loaded in the R environment:
Example
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
Install new packages
To install new packages, you can use theinstall.packages()function, with the following format:
install.packages("要安装的包名")
We can directly set the package name and obtain it fromCRANthe website. In the following example, we load the XML package:
# 安装 XML 包
install.packages("XML")
Alternatively, we can directlyCRANdownload the relevant package from the website and install it locally:
install.packages("./XML_3.98-1.3.zip")In China, we generally recommend using domestic mirrors. The following example uses the University of Science and Technology of China (USTC) source for installation:
# 安装 XML 包
install.packages("XML", repos = "https://mirrors.ustc.edu.cn/CRAN/")
One of the CRAN (The Comprehensive R Archive Network) mirror source configuration files is .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 packages
Newly installed packages need to be loaded into the R environment before they can be used. The format is as follows:
library("包名")
The following example loads the XML package:
library("XML") Other extensions