掌握了R基本的一些概念,今天咱们来简单了解下R包,R包安装、加载与更新
安装R包的几种方式
从CRAN中安装R包
首先是修改镜像,本身就在镜像范围内的就不用了,修改清华镜像站
site="https://mirrors.tuna.tsinghua.edu.cn/CRAN"
install.packages("ggplot2", repo=site) #单个包安装
install.packages(c("WGCNA", "factoextra"))
install.packages(c("ggplot2", "pheatmap","ggpubr")) #多个包安装
ins_pac = c("DO.db", "fgsea", "qvalue", "ggforce", "DOSE", "ggraph", "GOSemSim", "biomaRt", "enrichplot", "GenomicFeatures", "gridBase", "rtracklayer", "TxDb.Hsapiens.UCSC.hg19.knownGene")
install.packages("ins_pac", repo=site) #也完全可以多个R包批量安装
安装bioconductor包
这里不推荐使用biocLite()进行安装了,据说是可能会不安全哈,个人也碰到几次报错的情况,先安装BiocManager这个CRAN包,然后直接使用BiocManager::install()进行安装
install.packages("BiocManager")
BiocManager::install(c("GSEABase","GSVA","clusterProfiler" ),ask = F,update = F)
BiocManager::install(c("GEOquery","limma","impute" ),ask = F,update = F)
BiocManager::install(c("genefu","org.Hs.eg.db","hgu133plus2.db" ),ask = F,update = F)
BiocManager::version() #使用version()查看Bioconductor版本
安装github中的包
# 下载ggvegan包
library("devtools")
install_github("joey711/phyloseq")
devtools::install_github("gavinsimpson/ggvegan")
# 判断devtools工具是否存在,选择是否需要安装,因为很大。
require(devtools)
install_github("ggvegan")
if (!requireNamespace("devtools", quietly = TRUE))
install.packages("devtools")
devtools::install_github("calligross/ggthemeassist")
# 安装开发版(连github不稳定有时间下载失败,多试几次可以成功)
devtools::install_github("phyloseq", build_vignettes = TRUE)
# 安装新功能最优版
devtools::install_github("phyloseq", ref = "optimization")
# 安装固定版本的包
install_version("igraph", version = "0.6.5", repo="http://mirrors.tuna.tsinghua.edu.cn/CRAN/")
# 当不记得github仓库号之后,使用下面包githubinstall安装github包
install.packages('gdtools') #已发布至CRAN
library(githubinstall)
# 如果以上都还没有解决您的问题,还有办法我们下载github上的包,可能经常性的打不# 开,R中无法下载,甚至手动克隆都有可能随时中断。
# 无法下载得到github包,或者无法安装后,将github包手动下载下来,解压之后定位文件夹名称后安装
install.packages("C:/Users/xiaoqi/Desktop/hrbrthemes-master/", repos = NULL, type = "source")
install.packages("C:/Users/xiaoqi/Desktop/microbiomeutilities-master/", repos = NULL, type = "source")
library(microbiomeutilities)
R语言载入包
.cran_packages <- c("ggplot2", "gridExtra")
.bioc_packages <- c("dada2", "msa", "phyloseq")
# Load packages into session
sapply(c(.cran_packages, .bioc_packages), require, character.only = TRUE)
定期升级R及所有R包
# 升级R的版本
install.packages("installr")
library(installr) #按着提示点击完就升级完啦
# 升级所有R包
install.packages("rvcheck")
library(rvcheck)
update.packages()
update_all()
查看默认载入的包
getOption("defaultPackages") #查看启动R时自动载入的包。
.libPaths() #查看包的安装目录
library() #查看已经安装的包目录
查看R中已经载入的包
sessionInfo() #查看R中载入的包
查看”package”中的所有对象
ls("package:package") #查看"package"中的所有对象
最后附上生信技能树教程里面的成熟一套安装代码,里面大部分R包都是医生做科研能用上的哈
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if(! require("devtools")) install.packages("devtools")
if(! require("reshape2")) install.packages("reshape2")
if(! require("ggplot2")) install.packages("ggplot2")
if(! require("pheatmap")) install.packages("pheatmap")
if(! require("ggfortify")) install.packages("ggfortify")
if(! require("stringr")) install.packages("stringr")
if(! require("survival")) install.packages("survival")
if(! require("survminer")) install.packages("survminer")
if(! require("lars")) install.packages("lars")
if(! require("glmnet")) install.packages("glmnet")
if(! require("timeROC")) install.packages("timeROC")
if(! require("ggpubr")) install.packages("ggpubr")
if(! require("randomForest")) install.packages("randomForest")
if(! require("ROCR")) install.packages("ROCR")
if(! require("Hmisc")) install.packages("Hmisc")
if(! require("caret")) install.packages("caret")
# if(! require("genefilter")) install.packages("genefilter")
if(! require("ggstatsplot")) install.packages("ggstatsplot")
### 下面的包是为了临床三线表
if(! require("tableone")) install.packages("tableone")
## 网络不好,就不要安装了。
## 而且Windows电脑安装 rJava 也经常是需要人指导的。
# https://github.com/rstudio/rstudio/issues/2254
if(! require("rJava")) install.packages("rJava")
if(require('rJava')){
# https://cran.r-project.org/src/contrib/Archive/ReporteRs/
if(! require("ReporteRs")) install.packages("ReporteRs")
devtools::install_github('davidgohel/ReporteRsjars')
devtools::install_github('davidgohel/ReporteRs')
}
library(devtools)
source("http://bioconductor.org/biocLite.R")
## 如果你的网络实在是太差,试试看:
# install.packages("BiocInstaller",repos="http://bioconductor.org/packages/3.7/bioc")
## 很可惜你在中国大陆,不得不承受这个痛苦。
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
library('BiocInstaller')
if(! require('edgeR')){
BiocManager::install(c('airway','DESeq2','edgeR','limma'))
}
if(! require("CLL")) BiocManager::install("CLL")
if(! require("org.Hs.eg.db")) BiocManager::installe('org.Hs.eg.db')
library(BiocInstaller)
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
if(! require("maftools")) BiocManager::install("maftools")
if(! require("RTCGA")) BiocManager::install("RTCGA")
if(! require("RTCGA.clinical")) BiocManager::install("RTCGA.clinical")
# https://bioconductor.org/packages/3.6/data/experiment/src/contrib/RTCGA.clinical_20151101.8.0.tar.gzn
if(! require("RTCGA.miRNASeq")) BiocManager::install("RTCGA.miRNASeq")
if(! require("maftools")) BiocManager::install("maftools")
if(! require("genefilter")) BiocManager::install("genefilter")
# Then from : https://github.com/ShixiangWang
# You don't need run the codes below, I will explain to you face to face.
source("http://bioconductor.org/biocLite.R")
packs = c("devtools", "reshape2", "ggplot2", "pheatmap", "ggfortify", "stringr", "survival",
"survminer", "lars", "glmnet", "timeROC", "ggpubr", "randomForest", "ROCR", "genefilter",
"Hmisc", "caret", "airway","DESeq2","edgeR","limma", "CLL", "org.Hs.eg.db", "maftools")
if(! require(pacman)) install.packages("pacman", dependencies = TRUE)
pacman::p_load(packs, dependencies=TRUE, character.only = TRUE)
# check
pacman::p_loaded(packs, character.only = TRUE)
all(pacman::p_loaded(packs, character.only = TRUE))