掌握了R基本的一些概念,今天咱们来简单了解下R包,R包安装、加载与更新

安装R包的几种方式

从CRAN中安装R包

首先是修改镜像,本身就在镜像范围内的就不用了,修改清华镜像站

  1. site="https://mirrors.tuna.tsinghua.edu.cn/CRAN"
  2. install.packages("ggplot2", repo=site) #单个包安装
  3. install.packages(c("WGCNA", "factoextra"))
  4. install.packages(c("ggplot2", "pheatmap","ggpubr")) #多个包安装
  5. ins_pac = c("DO.db", "fgsea", "qvalue", "ggforce", "DOSE", "ggraph", "GOSemSim", "biomaRt", "enrichplot", "GenomicFeatures", "gridBase", "rtracklayer", "TxDb.Hsapiens.UCSC.hg19.knownGene")
  6. install.packages("ins_pac", repo=site) #也完全可以多个R包批量安装

安装bioconductor包

这里不推荐使用biocLite()进行安装了,据说是可能会不安全哈,个人也碰到几次报错的情况,先安装BiocManager这个CRAN包,然后直接使用BiocManager::install()进行安装

  1. install.packages("BiocManager")
  2. BiocManager::install(c("GSEABase","GSVA","clusterProfiler" ),ask = F,update = F)
  3. BiocManager::install(c("GEOquery","limma","impute" ),ask = F,update = F)
  4. BiocManager::install(c("genefu","org.Hs.eg.db","hgu133plus2.db" ),ask = F,update = F)
  5. BiocManager::version() #使用version()查看Bioconductor版本

安装github中的包

  1. # 下载ggvegan包
  2. library("devtools")
  3. install_github("joey711/phyloseq")
  4. devtools::install_github("gavinsimpson/ggvegan")
  5. # 判断devtools工具是否存在,选择是否需要安装,因为很大。
  6. require(devtools)
  7. install_github("ggvegan")
  8. if (!requireNamespace("devtools", quietly = TRUE))
  9. install.packages("devtools")
  10. devtools::install_github("calligross/ggthemeassist")
  11. # 安装开发版(连github不稳定有时间下载失败,多试几次可以成功)
  12. devtools::install_github("phyloseq", build_vignettes = TRUE)
  13. # 安装新功能最优版
  14. devtools::install_github("phyloseq", ref = "optimization")
  15. # 安装固定版本的包
  16. install_version("igraph", version = "0.6.5", repo="http://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  17. # 当不记得github仓库号之后,使用下面包githubinstall安装github包
  18. install.packages('gdtools') #已发布至CRAN
  19. library(githubinstall)
  20. # 如果以上都还没有解决您的问题,还有办法我们下载github上的包,可能经常性的打不# 开,R中无法下载,甚至手动克隆都有可能随时中断。
  21. # 无法下载得到github包,或者无法安装后,将github包手动下载下来,解压之后定位文件夹名称后安装
  22. install.packages("C:/Users/xiaoqi/Desktop/hrbrthemes-master/", repos = NULL, type = "source")
  23. install.packages("C:/Users/xiaoqi/Desktop/microbiomeutilities-master/", repos = NULL, type = "source")
  24. library(microbiomeutilities)

R语言载入包

  1. .cran_packages <- c("ggplot2", "gridExtra")
  2. .bioc_packages <- c("dada2", "msa", "phyloseq")
  3. # Load packages into session
  4. sapply(c(.cran_packages, .bioc_packages), require, character.only = TRUE)

定期升级R及所有R包

  1. # 升级R的版本
  2. install.packages("installr")
  3. library(installr) #按着提示点击完就升级完啦
  4. # 升级所有R包
  5. install.packages("rvcheck")
  6. library(rvcheck)
  7. update.packages()
  8. update_all()

查看默认载入的包

  1. getOption("defaultPackages") #查看启动R时自动载入的包。
  2. .libPaths() #查看包的安装目录
  3. library() #查看已经安装的包目录

查看R中已经载入的包

  1. sessionInfo() #查看R中载入的包

查看”package”中的所有对象

  1. ls("package:package") #查看"package"中的所有对象

最后附上生信技能树教程里面的成熟一套安装代码,里面大部分R包都是医生做科研能用上的哈

  1. options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
  2. if(! require("devtools")) install.packages("devtools")
  3. if(! require("reshape2")) install.packages("reshape2")
  4. if(! require("ggplot2")) install.packages("ggplot2")
  5. if(! require("pheatmap")) install.packages("pheatmap")
  6. if(! require("ggfortify")) install.packages("ggfortify")
  7. if(! require("stringr")) install.packages("stringr")
  8. if(! require("survival")) install.packages("survival")
  9. if(! require("survminer")) install.packages("survminer")
  10. if(! require("lars")) install.packages("lars")
  11. if(! require("glmnet")) install.packages("glmnet")
  12. if(! require("timeROC")) install.packages("timeROC")
  13. if(! require("ggpubr")) install.packages("ggpubr")
  14. if(! require("randomForest")) install.packages("randomForest")
  15. if(! require("ROCR")) install.packages("ROCR")
  16. if(! require("Hmisc")) install.packages("Hmisc")
  17. if(! require("caret")) install.packages("caret")
  18. # if(! require("genefilter")) install.packages("genefilter")
  19. if(! require("ggstatsplot")) install.packages("ggstatsplot")
  20. ### 下面的包是为了临床三线表
  21. if(! require("tableone")) install.packages("tableone")
  22. ## 网络不好,就不要安装了。
  23. ## 而且Windows电脑安装 rJava 也经常是需要人指导的。
  24. # https://github.com/rstudio/rstudio/issues/2254
  25. if(! require("rJava")) install.packages("rJava")
  26. if(require('rJava')){
  27. # https://cran.r-project.org/src/contrib/Archive/ReporteRs/
  28. if(! require("ReporteRs")) install.packages("ReporteRs")
  29. devtools::install_github('davidgohel/ReporteRsjars')
  30. devtools::install_github('davidgohel/ReporteRs')
  31. }
  32. library(devtools)
  33. source("http://bioconductor.org/biocLite.R")
  34. ## 如果你的网络实在是太差,试试看:
  35. # install.packages("BiocInstaller",repos="http://bioconductor.org/packages/3.7/bioc")
  36. ## 很可惜你在中国大陆,不得不承受这个痛苦。
  37. options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
  38. library('BiocInstaller')
  39. if(! require('edgeR')){
  40. BiocManager::install(c('airway','DESeq2','edgeR','limma'))
  41. }
  42. if(! require("CLL")) BiocManager::install("CLL")
  43. if(! require("org.Hs.eg.db")) BiocManager::installe('org.Hs.eg.db')
  44. library(BiocInstaller)
  45. options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
  46. if(! require("maftools")) BiocManager::install("maftools")
  47. if(! require("RTCGA")) BiocManager::install("RTCGA")
  48. if(! require("RTCGA.clinical")) BiocManager::install("RTCGA.clinical")
  49. # https://bioconductor.org/packages/3.6/data/experiment/src/contrib/RTCGA.clinical_20151101.8.0.tar.gzn
  50. if(! require("RTCGA.miRNASeq")) BiocManager::install("RTCGA.miRNASeq")
  51. if(! require("maftools")) BiocManager::install("maftools")
  52. if(! require("genefilter")) BiocManager::install("genefilter")
  53. # Then from : https://github.com/ShixiangWang
  54. # You don't need run the codes below, I will explain to you face to face.
  55. source("http://bioconductor.org/biocLite.R")
  56. packs = c("devtools", "reshape2", "ggplot2", "pheatmap", "ggfortify", "stringr", "survival",
  57. "survminer", "lars", "glmnet", "timeROC", "ggpubr", "randomForest", "ROCR", "genefilter",
  58. "Hmisc", "caret", "airway","DESeq2","edgeR","limma", "CLL", "org.Hs.eg.db", "maftools")
  59. if(! require(pacman)) install.packages("pacman", dependencies = TRUE)
  60. pacman::p_load(packs, dependencies=TRUE, character.only = TRUE)
  61. # check
  62. pacman::p_loaded(packs, character.only = TRUE)
  63. all(pacman::p_loaded(packs, character.only = TRUE))