R包的安装与更新
一、R包的安装
R和bioconductor的主服务器都在国外,设置镜像加快下载速度。
## ====修改镜像====
options(repos=c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
## 检查镜像是否修改成功。
options()$BioC_mirror
options()$repos
##====(1)批量安装Packages====
# pacman包文档地址 https://www.rdocumentation.org/packages/pacman/versions/0.5.1
# pacman conveniently wraps library and package related functions and names them in an intuitive and consistent fashion.
packs = c("haven", "reshape2", "ggplot2", "pheatmap", "openxlsx", "dplyr", "survival", "survminer", "survIDINRI", "LDheatmap", "packHV", "broom", "forestplot", "Hmisc", "CPE", "nricens", "compareC", "gdsfmt","SNPRelate","survcomp", "biomaRt", "validstats", "phenoscanner")
if(! require(pacman)) install.packages("pacman", dependencies = TRUE)
pacman::p_load(packs, dependencies=TRUE, character.only = TRUE) # p_load(): Load and Install Packages
# check
pacman::p_loaded(packs, character.only = TRUE)
all(pacman::p_loaded(packs, character.only = TRUE))
如果想知道R包的来源,科学上网[1]搜索一下即可。如,ggplot2 R package。
##====(2)根据不同的Repository安装Packages====
# 1) CRAN----
if(! require(CPE)) install.packages("CPE")
# 报错:Failed with error: 'package 'Hmisc’ 4.2.0 was found, but >= 4.3.0 is required by 'rms’’
# remove.packages("Hmisc") #版本过低,卸载后重装
# if(! require(Hmisc)) install.packages("Hmisc")
if(! require(statmod)) install.packages("statmod")
if(! require(rmutil)) install.packages("rmutil")
if(! require(haplo.stats)) install.packages("haplo.stats")
# 2) Bioconducter----
if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager")
BiocManager::install("gdsfmt")
BiocManager::install(c("GenomicRanges", "Organism.dplyr"))
# 3) GitHub----
if(! require(devtools)) install.packages("devtools")
library(devtools)
install_github("phenoscanner/phenoscanner") #query the PhenoScanner database
# 报错:Error: Failed to install 'phenoscanner' from GitHub: Does not appear to be an R package (no DESCRIPTION)
# 解决参考:https://github.com/r-lib/devtools/issues/1939
# You seem to have a non-standard tar on your PATH. You could try setting Sys.setenv("TAR" = "internal") to use R's internal tar instead.
Sys.setenv("TAR" = "internal") # install_github("phenoscanner/phenoscanner")
library(phenoscanner)
# ====(3)本地下载安装Packages====
# 1)下载Rtools工具(安装过程默认自动配置环境变量)https://cran.r-project.org/bin/windows/Rtools/ ;
# 2) 指定安装包所在目录(待安装包下载到本地的位置)。
# 示例R包下载地址:http://individual.utoronto.ca/osaarela/
install.packages("D:/Program Files/R/R-3.5.2/bin/x64/validstats_1.4.tar.gz", repos = NULL, type = "source") # 注意R包的编码类型
install.packages("D:/Program Files/R/R-3.6.1/bin/x64/phenoscanner.zip", repos = NULL,type = "win.binary")
二、R包的更新
# 加载R包
require("ggplot2") # 能够返回逻辑值,若包能够被加载利用,则返回T,否则返回F;常用于条件判断
library("ggplot2") # 不返回逻辑值
# 压缩R包加载信息,使显示更加简洁
suppressPackageStartupMessages({
library(statmod)
library(rmutil)
library(haplo.stats)
library(gdsfmt)
})
# 查看使用示例
??gdsfmt
browseVignettes("gdsfmt")
R.version.string #查看R版本信息
sessionInfo() #查看R版本号、R运行的平台、加载的包及其安装的包和版本号
.libPaths() # 查看R包的安装位置
library() # 查看所有已安装的R包
packageVersion("ggplot2") #查看某个包的版本号
#查看已经安装的包及其版本号和安装路径
installed.packages()[,c('Package','Version','LibPath')]
available.packages() # 查看CRAN可以安装哪些包
BiocManager::available() # 搜索可用Bioconductor R包
#查看ggplot2包及其版本号和安装路径
installed.packages()["ggplot2",c('Package','Version','LibPath')]
#查看已经加载的包
(.packages())
search()
#查看某个包的详细信息
packageDescription("ggplot2")
library(help="ggplot2")
help("ggplot") #查看某个函数属于哪个包
args(lm) #查看函数的参数
example(lm) #自动运行该函数帮助文档中的例子
# 升级R
if(! require(installr)) install.packages("installr")
updateR()
# install Rtools
install.Rtools()
# 重新安装ggplot2包,是安装最新R包版本
install.packages("ggplot2")
update.packages("ggplot2") # 更新ggplot2包
update.packages(ask = F) #更新所有R包
BiocManager::install() # 更新所有已安装的 R 包到最新版本
BiocManager::version() # 查看 Bioconductor 的版本
BiocManager::available("BSgenome.Hsapiens") # 根据正则表达式过滤包名称
# 安装特定版本的R包
devtools::install_version("survminer", version = "0.4.2")
packageVersion("survminer") # 当前版本查看
packageurl = "https://cran.r-project.org/src/contrib/Archive/survminer/survminer_0.4.2.tar.gz"
install.packages(packageurl, repos = NULL, type = "source")
# 使用rvcheck::update_all()可以自动更新CRAN, Bioconductor和Github上的R包
rvcheck::update_all()
#从环境中移除R包
detach(name="package:plyr")
#从下载库中卸载R包
remove.packages("plyr")
# 注意:移除R包,可以library()重新加载到R环境中,
# 卸载R包,则不能重新加载进来,必须重新下载该包才能重新加载到R环境中。
赞 (0)