编程学习

不同来源的R包安装

2023-10-24  本文已影响0人  myshu

1、CRAN来源包安装(install.packages)

这种方式是最常见的,可以一次安装一个包或者多个包:

# 示例
install.packages("ggsci")
install.packages(c('ggplot2', 'pheatmap', 'ggpubr', 'ggnewscale'))

有时候需要安装指定的版本,可以直接指定下载的url或者下载压缩包到本地进行安装:

# 指定url
install.packages("http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_1.0.1.tar.gz", repos=NULL, type="source")
# 下载到本地
install.packages("BiocInstaller_1.20.1.tar.gz", repos = NULL)

还有时候安装一些包的时候,需要一些依赖,可以使用withr::with_makevars来指定依赖进行安装。

with_makevars的功能:Temporarily change contents of an existing Makevars file.

具体可以查看安装时候的报错,比如有些.h文件等找不到了这种,一般都会有一些提示,然后指定下再安装就没问题了。(建议报错网络搜索下看缺啥依赖)
如下是我在安装devtools包依赖的时候报错的几个包的示例:

# 安装devtools包依赖
withr::with_makevars(c(CPPFLAGS="-I/miniconda3/envs/seurat_v5_2023/include/freetype2/"), install.packages("systemfonts"))
withr::with_makevars(c(CPPFLAGS="-I/miniconda3/envs/seurat_v5_2023/include/harfbuzz/ -I/miniconda3/envs/seurat_v5_2023/include/fribidi/ -I/miniconda3/envs/seurat_v5_2023/include/freetype2/"), install.packages("textshaping"))
withr::with_makevars(c(CPPFLAGS="-I/miniconda3/envs/seurat_v5_2023/include/freetype2/"), install.packages("ragg"))

2、github来源包安装

remotes和devtools都可以安装,两者的区别可以参考:https://mp.weixin.qq.com/s/vSDqpwHkZjMZz6KJowV8jA
从 Github 安装包,两个函数都可以完成,但 remotes::install_github() 通常更快、更轻量级,如果安装包的话还是优先remotes。remotes还能安装来自从 Github、GitLab、Bitbucket 和 SVN 等其他来源安装包的功能

# 从github安装
# 不用导入
remotes::install_github('ZJUFanLab/scCATCH')
# 先导入
library(remotes)
install_github("satijalab/seurat-data", "seurat5")
install_github("Coolgenome/iTALK", build_vignettes = TRUE)

# devtools安装类似
#library(devtools)
devtools::install_github('ZJUFanLab/scCATCH')

3、Bioconductor来源包安装

搜索R包网站:https://www.bioconductor.org/
在 R==3.5(Bioconductor-3.7) 前,Bioconductor 都是通过 biocLite 安装相关的 R 包:

source("https://bioconductor.org/biocLite.R")
biocLite(pkg_name)

从 R-3.5(Bioconductor-3.8)起,Bioconductor 改成了BiocManager包来安装

# 通过bioconductor安装
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("pcaMethods")

4、conda安装R包

搜索R包的网站:https://anaconda.org/
如果对应的R包在conda中已有安装包,也可以考虑使用conda 来安装。一般就是r-包名称或者bioconductor-包名称这种格式。

# 通过conda安装
conda install r-base
conda install r-ggplot2 

最后,基本上任何来源的包,如果安装时是由于网络等下载不下来导致失败,都可以直接下载到本地(包括自己写的包),然后使用install.packages()来本地进行安装。
安装时注意:

如有描述不当或者错误,欢迎大家批评指正!

上一篇 下一篇

猜你喜欢

热点阅读