Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R install.packages() Function - Installing Extension Packages

R install.packages() Function - Installing Extension Packages

R Language Examples R Language Examples

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:

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

(

)

print

(

"First 5 installed packages:"

)

print

(

head

(

installed

[

,

c

(

"Package"

,

"Version"

)

]

,

5

)

)

View CRAN mirrors

print

(

"Current CRAN mirror:"

)

print

(

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 R Language Examples