R install.packages() Function - Installing Extension Packages
The R install.packages() function is used to install R extension packages from CRAN or other mirror sources.
R's ecosystem has more than 20,000 packages, and extension packages are the core of R's powerful features.
The syntax of the install.packages() function is as follows:
install.packages(pkgs, repos = getOption("repos"),
dependencies = NA)
Parameter Description:
-
pkgs The names of packages to install (a character vector).
-
repos The CRAN mirror source address.
-
dependencies Whether to install dependent packages. TRUE means install all dependencies.
Example
Installation of common packages (example, actually downloads from the network)
install.packages("ggplot2") # Install a single package
install.packages(c("dplyr", "tidyr")) # Install multiple packages
View currently installed packages
installed
<-
installed.packages
(
)
(
"First 5 installed packages:"
)
(
head
(
installed
[
,
c
(
"Package"
,
"Version"
)
]
,
5
)
)
View CRAN mirrors
(
"Current CRAN mirror:"
)
(
getOption
(
"repos"
)
)
Set a mirror (Tsinghua source recommended for Chinese users)
options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
The output of the code above is:
[1] "已安装包的前 5 个:"
Package Version
base "base" "4.3.0"
boot "boot" "1.3-28"
...
[1] "当前 CRAN 镜像:"
CRAN
"https://cran.rstudio.com/"
When using R in China to install packages, it is recommended to set a CRAN mirror from Tsinghua or USTC to speed up downloads.
R Language Examples