R 包



R 包是 R 函式、編譯程式碼和示例資料的集合。它們儲存在 R 環境中名為 **“library”** 的目錄下。預設情況下,R 在安裝過程中會安裝一組包。稍後,當需要用於某些特定目的時,會新增更多包。當我們啟動 R 控制檯時,預設情況下只有預設包可用。其他已安裝的包必須顯式載入才能被將要使用它們的 R 程式使用。

R 語言中所有可用的包都列在 R 包。

以下是用於檢查、驗證和使用 R 包的命令列表。

檢查可用的 R 包

獲取包含 R 包的庫位置

.libPaths()

當我們執行上述程式碼時,它會產生以下結果。它可能會根據您電腦的本地設定而有所不同。

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

獲取所有已安裝包的列表

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" 

安裝新包

有兩種方法可以新增新的 R 包。一種是從 CRAN 目錄直接安裝,另一種是將包下載到本地系統並手動安裝。

直接從 CRAN 安裝

以下命令直接從 CRAN 網頁獲取包並在 R 環境中安裝包。您可能會被提示選擇最近的映象。選擇適合您所在位置的映象。

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

手動安裝包

訪問連結 R 包 下載所需的包。將包儲存為本地系統中合適位置的 **.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")

將包載入到庫中

在程式碼中使用包之前,必須將其載入到當前 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")
廣告