English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

包R

包R

Packages are a collection of R functions, sample data, precompiled code, including R programs, comment documents, examples, test data, etc.

R language related packages are generally stored in the "library" directory under the installation directory, by default, some commonly used packages are brought by the R language installation, of course, we can also add some packages to be used later.

You can find the complete set of R language related packages here:https://cran.r-project.org/web/packages/available_packages_by_name.html

Next, we mainly introduce how to install R language packages.

View the installation directory of R packages

We can use the following functions to view the installation directory of R packages:

> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"
>

View installed packages

We can use the following functions to view the installed packages:

library()

Output result 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
                        e Fonti
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

Visualizzazione dei pacchetti caricati

Possiamo utilizzare le seguenti funzioni per visualizzare i pacchetti caricati nell'ambiente di compilazione:

> search()
".GlobalEnv" "package:stats" "package:graphics" 
"package:grDevices" "package:utils" "package:datasets" 
[7] "package:methods" "Autoloads" "package:base"

安装新包

安装新包可以使用 install.packages() 函数,格式如下:

install.packages("要安装的包名")

我们可以直接设置包名,从  CRAN 网站上获取包,如下示例我们载入XML包:

# 安装 XML 包
install.packages("XML")

或者我们可以直接在 CRAN 上下载相关包,直接在本地安装:

install.packages("./XML_3.98-1.3.zip")

我们国内一般建议大家使用国内镜像,以下示例使用清华源进行安装:

# 安装 XML 包
install.packages("XML", repos = "https://mirrors.ustc.edu.cn/CRAN/")

CRAN (The Comprehensive R Archive Network) 镜像源配置文件之一是 .Rprofile (linux下位于 ~/.Rprofile )。

在文末添加如下语句:

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))

打开R即可使用该CRAN镜像源安装R软件包。

使用包

新安装的包需要先载入R编译环境中才可以使用,格式如下:

library("包名")

以下示例载入XML包:

library("XML")