R 简明教程

R - Packages

R 包是 R 函数、已编译代码和样本数据的集合。它们被存储在 R 环境中名为 "library" 的目录下。默认情况下,R 在安装过程中安装了一组包。当它们被用于某些特定目的时,后期还会添加更多的包。当我们启动 R 控制台时,默认情况下只有默认包可用。必须显式加载其他已安装的包,才能让将要使用它们的 R 程序访问它们。

R 语言中所有可用的包均在 R Packages. 列出。

下方是用于检查、验证和使用 R 包的命令列表。

Check Available R Packages

获取包含 R 包的库位置

.libPaths()

当我们执行以上代码时,它会生成以下结果。结果可能根据电脑的本地设置有所不同。

[2] "C:/Program Files/R/R-3.2.2/library"

Get the list of all the packages installed

library()

当我们执行以上代码时,它会生成以下结果。结果可能根据电脑的本地设置有所不同。

Packages in library ‘C:/Program Files/R/R-3.2.2/library’:

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
Matrix                  Sparse and Dense Matrix Classes and Methods
methods                 Formal Methods and Classes
mgcv                    Mixed GAM Computation Vehicle with GCV/AIC/REML
                        Smoothness Estimation
nlme                    Linear and Nonlinear Mixed Effects Models
nnet                    Feed-Forward Neural Networks and Multinomial
                        Log-Linear Models
parallel                Support for Parallel computation in R
rpart                   Recursive Partitioning and Regression Trees
spatial                 Functions for Kriging and Point Pattern
                        Analysis
splines                 Regression Spline Functions and Classes
stats                   The R Stats Package
stats4                  Statistical Functions using S4 Classes
survival                Survival Analysis
tcltk                   Tcl/Tk Interface
tools                   Tools for Package Development
utils                   The R Utils Package

获取 R 环境中当前加载的所有包

search()

当我们执行以上代码时,它会生成以下结果。结果可能根据电脑的本地设置有所不同。

[1] ".GlobalEnv"        "package:stats"     "package:graphics"
[4] "package:grDevices" "package:utils"     "package:datasets"
[7] "package:methods"   "Autoloads"         "package:base"

Install a New Package

有两种添加新 R 包的方法。一种是直接从 CRAN 目录安装,另一种是将包下载至本地系统并手动安装。

Install directly from CRAN

以下命令直接从 CRAN 网页获取包,并将包安装至 R 环境。系统可能会提示你选择最近的镜像。选择一个适合你所在位置的镜像。

 install.packages("Package Name")

# Install the package named "XML".
 install.packages("XML")

Install package manually

前往链接 R Packages 下载所需的包。将包另存为 .zip 文件至本地系统中的适当位置。

现在,你可以运行以下命令以在 R 环境中安装此包。

install.packages(file_name_with_path, repos = NULL, type = "source")

# Install the package named "XML"
install.packages("E:/XML_3.98-1.3.zip", repos = NULL, type = "source")

Load Package to Library

在代码中使用包之前,必须先将其加载至当前 R 环境。你还可以加载之前已安装但当前环境中不可用的包。

使用以下命令加载包 −

library("package Name", lib.loc = "path to library")

# Load the package named "XML"
install.packages("E:/XML_3.98-1.3.zip", repos = NULL, type = "source")