R 语言 包

  • 是R函数集合,编译后的代码和示例数据的集合。它们存储在R环境中的“library”目录下。默认情况下,R在安装过程中安装一组软件包。当出于某些特定目的需要它们时,会在以后添加更多软件包。当我们启动R控制台时,默认情况下仅默认软件包可用。已安装的其他软件包必须显式加载,以供将要使用它们的R程序使用。
    R软件包中列出了所有可用R语言提供的软件包。
    以下是用于检查,验证和使用R软件包的命令列表。
  • 检查可用的R包

    获取包含R包的库位置
     
    .libPaths()
    
    尝试一下
    当我们执行上面的代码时,它将产生以下结果。它可能会因计算机的本地设置而异。
     
    [1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" 
    [3] "/usr/lib/R/library" 
    
    获取所有已安装软件包的列表
     
    library()
    
    尝试一下
    当我们执行上面的代码时,它将产生以下结果。它可能会因计算机的本地设置而异。
    
    Packages in library ‘/usr/local/lib/R/site-library’:
    
    docopt Command-Line Interface Specification Language
    
    Packages in library ‘/usr/lib/R/site-library’:
    
    littler R at the Command-Line via 'r'
    
    Packages in library ‘/usr/lib/R/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 Automatic
    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 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")
    
  • 将包加载到库

    必须先将程序包加载到当前的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")