R Package
Packages are collections of R functions, example data, and precompiled code, including R programs, documentation, examples, test data, etc.
Packages related to R language are generally stored in the "library" directory under the installation directory. By default, R comes with some commonly used packages after installation, and we can also add some packages we need to use in the later stage.
The complete list of R packages can be found at: [https://cran.r-project.org/web/packages/available_packages_by_name.html](https://cran.r-project.org/web/packages/available_packages_by_name.html)
Next, we will mainly introduce how to install R packages.
### View R Package Installation Directory
We can use the following function to view the installation directory of R packages:
## Example
> .libPaths()
"/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 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 PackageKernSmooth 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 packages loaded in the compilation environment:
## Example
>search()
".GlobalEnv""package:stats""package:graphics"
"package:grDevices""package:utils""package:datasets"
"package:methods""Autoloads""package:base"
* * *
## Install New Packages
To install new packages, you can use the **install.packages()** function, with the following format:
install.packages("package name to install")
We can directly set the package name to get the package from the (https://cran.r-project.org/web/packages/available_packages_by_name.html) website. In the following example, we load the XML package:
# Install XML package install.packages("XML")
Or we can directly download the related package from (https://cran.r-project.org/web/packages/available_packages_by_name.html) and install it locally:
install.packages("./XML_3.98-1.3.zip")
For users in China, it is recommended to use domestic mirrors. The following example uses the University of Science and Technology of China source for installation:
# Install XML package install.packages("XML", repos = "https://mirrors.ustc.edu.cn/CRAN/")
One of the CRAN (The Comprehensive R Archive Network) mirror 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 and you can use this CRAN mirror to install R packages.
### Use Packages
Newly installed packages need to be loaded into the R compilation environment before they can be used, with the following format:
library("package name")
The following example loads the XML package:
library("XML")
YouTip