介绍

R语言提供的大量R包为众多研究者提供了足够的工具,但是如何安装R包是很多人在使用R语言做数据分析时候所面临的问题之一。接下来介绍如何大规模安装所需要的R包。

常用安装

  1. install.packages函数是我们常用的安装R包的方式,需要注意的是这些R包必须是在CRAN仓库中,否则安装将会失败。安装方式可以将单个包作为变量传输进入,也可以以向量模式传递多个包。
  1. # Installation of required packages in single model
  2. install.packages("tidyverse")
  3. install.packages("ggplot2")
  4. install.packages("dplyr")
  5. install.packages("tidyr")
  6. # mulitple packages in one command line
  7. install.packages(c("tidyverse", "ggplot2", "dplyr", "tidyr"))
  8. # load packages
  9. library("tidyverse")
  10. library("ggplot2")
  11. library("dplyr")
  12. library("tidyr")
  1. 这里不得不提的是另一个存放R包的网址bioconductor。该项目是存放了大量用于生物研究的R包,很多做生物信息分析的人都会使用里面提供的R包。它的安装包是通过BiocManager包提供的install函数实现的。

The mission of the Bioconductor project is to develop, support, and disseminate free open source software that facilitates rigorous and reproducible analysis of data from current and emerging biological assays.

  1. # Installation of required packages in single model
  2. BiocManager::install("DESeq2")
  3. BiocManager::install("gsva")
  4. # mulitple packages in one command line
  5. BiocManager::install(c("DESeq2", "gsva"))
  6. # load packages
  7. library("DESeq2")
  8. library("gsva")
  1. 还有一类是开发者把未经过CRAN或bioconductor等审核过但存放在如github, gitlab等开源网站的R包,这类R包可以分别通过devtoolsremote包的 install_githubinstall_gitlab等函数安装。
  1. devtools::install_github("HuaZou/MyRtools")
  2. remotes::install_github("HuaZou/MyRtools")
  3. devtools::install_version("Rcpp", version = "1.0.4.6",repos = "[http://cran.us.r-project.org](http://cran.us.r-project.org)")
  1. 除了联网安装R包外,R还提供本地下载压缩包安装模式。
  1. install.packages("local/packagename.tar.gz", repos=NULL, type="source")

高效方式一

随着时间流逝,安装的R包也越来越多,如何快捷分辨出未安装过的R包就显得尤其重要。我们可以通过 installed.packages函数判断,并使用lapply函数分次安装所有的R包。构建函数,使其具有如下功能:

  1. 判断未安装R包;
  2. 使用 install.packagesBiocManager::install函数安装来源你不同的R包;
  3. 用lapply分别加载R包,并不输出加载过程中产生的信息。
  1. packages_CRAN <- c("tidyverse", "ggplot2", "dplyr", "tidyr")
  2. packages_biocond <- c("DESeq2", "gsva")
  3. InstallPackageFun <- function(packages=packages_CRAN , type="CRAN"){
  4. #packages=packages_CRAN
  5. #type="CRAN"
  6. # Install packages not yet installed
  7. installed_packages <- packages %in% rownames(installed.packages())
  8. if (any(installed_packages == FALSE)) {
  9. if(type == "CRAN"){
  10. lapply(packages[!installed_packages], install.packages)
  11. }else{
  12. lapply(packages[!installed_packages], BiocManager::install)
  13. }
  14. }
  15. # Packages Loading
  16. invisible(lapply(packages, library, character.only = TRUE))
  17. }
  18. InstallPackageFun(packages=packages_CRAN , type="CRAN")
  19. InstallPackageFun(packages=packages_biocond , type="bioconductor")

高效方式二

除了上面这种大规模安装未安装过的R包外,还可以通过已经构建好的R包内置函数安装,例如现在比较友好的R pacman,它提供的p_load函数其实可以看成是上述InstallPackageFun的升级版本。还有一个librarian包提供的shelf函数和p_load有类似的功能。

  • pacman
  1. install.packages("pacman")
  2. pacman::p_load(ggplot2, tidyr, dplyr)
  • librarian
  1. install.packages("librarian")
  2. librarian::shelf(ggplot2, HuaZou/MyRtools, DESeq2)

最后综上所述,安装R包除了来源不同,其实有时候还会考虑到版本问题,这里面的问题就比较多了,有时间再写吧。

Reference

参考文章如引起任何侵权问题,可以与我联系,谢谢。