以下为安装 R 包的常见方式汇总,涵盖了从 CRAN、GitHub、Bioconductor、本地文件等多种场景的安装方法,并附带一些常见问题的解决思路。
1. 从 CRAN 安装
1.1 基础方法
最常用、最简单的方式是直接通过 install.packages()
从 CRAN(Comprehensive R Archive Network) 安装:
install.packages("包名")
1.1.1 常见选项
-
repos:指定下载源,例如:
install.packages("包名", repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
-
dependencies:指定是否安装依赖包:
install.packages("包名", dependencies = TRUE)
这样会自动安装该包所依赖的其他包。
1.2 国内常用 CRAN 镜像地址
一般可以在 R 启动后通过以下命令临时设置镜像:
options(repos = c(CRAN = "https://mirrors.ustc.edu.cn/CRAN/"))
或者编辑 ~/.Rprofile
文件,加入:
local({
r <- getOption("repos")
r["CRAN"] <- "https://mirrors.ustc.edu.cn/CRAN/"
options(repos = r)
})
这样每次启动 R 时都会自动使用该镜像。
2. 从 Bioconductor 安装
Bioconductor 是面向生物信息学和基因组学的 R 包仓库。安装 Bioconductor 包需要使用 BiocManager
:
2.1 安装 BiocManager
如果尚未安装 BiocManager
,先从 CRAN 获取:
install.packages("BiocManager")
2.2 使用 BiocManager 安装 Bioconductor 包
例如安装包 DESeq2:
BiocManager::install("DESeq2")
也可以一次安装多个包:
BiocManager::install(c("GenomicFeatures", "AnnotationDbi"))
3. 从 GitHub 安装
对于尚未在 CRAN 上架或者需要安装最新开发版的包,可以通过 remotes
或 devtools
包从 GitHub 安装。
3.1 安装 remotes 或 devtools
install.packages("remotes")
# 或者
install.packages("devtools")
3.2 使用 remotes / devtools 安装
示例:从 GitHub 仓库安装包(假设仓库在 github_user/github_repo
):
remotes::install_github("github_user/github_repo")
# 例如
remotes::install_github("jinworks/CellChat")
devtools::install_github("github_user/github_repo")
也同理。
3.2.1 使用分支、tag、commit 安装
如果想要指定安装某个分支、特定 tag 或 commit,可以使用 ref
参数:
remotes::install_github("github_user/github_repo", ref = "dev_branch")
remotes::install_github("github_user/github_repo", ref = "v1.0.2")
remotes::install_github("github_user/github_repo", ref = "commit-hash")
4. 从本地文件安装
如果已经下载好了 .tar.gz
或 .zip
包文件,可以使用以下方式安装:
install.packages("路径/文件名.tar.gz", repos = NULL, type = "source")
或是:
install.packages("路径/文件名.zip", repos = NULL, type = "win.binary")
# 或从从GitHub上下载解压后得到文件 (我是用这个方法安装上的)
install.packages("路径/文件名", repos = NULL, type = "source")
其中:
- repos = NULL:表示安装本地文件
- type:在 Windows 上一般是
"win.binary"
,在 Mac/Linux 上通常是"source"
- 直接从GitHub上下载的由于它是源代码,而不是已编译好的 Windows 二进制包,需要指定 type =
"source"
,而非"win.binary"
5. 安装特定版本的 R 包
有时需要安装某个特定版本的包,可以使用 remotes
包中的 install_version()
函数。例如,要安装名为 dplyr 的指定版本:
remotes::install_version("dplyr", version = "0.8.3")
install_version()
会自动从 CRAN(或其镜像)下载该版本的二进制包或源文件进行安装。
6. 遇到的问题
在本地安装的时候输入以下指令:
install.packages(
"C:/Users/PC/Downloads/monocle3-master",
repos = NULL,
type = "source"
)
ERROR: dependencies 'SingleCellExperiment', 'assertthat', 'batchelor', 'BiocParallel', 'DelayedArray', 'DelayedMatrixStats', 'ggrastr', 'grr', 'HDF5Array', 'limma', 'pbmcapply', 'pheatmap', 'proxy', 'pscl', 'rsample', 'RhpcBLASctl', 'sf', 'slam', 'spdep', 'speedglm', 'SummarizedExperiment', 'viridis' are not available for package 'monocle3'
在安装 monocle3(源代码方式)时,R 会提示你缺少一系列依赖包,包括来自 CRAN 和 Bioconductor 的包。只有先安装好这些依赖,才能正常编译和安装 monocle3。
6.1. 安装依赖包
根据报错信息,缺少的依赖包如下(按来源大致分组):
-
Bioconductor 包
SingleCellExperiment
,batchelor
,BiocParallel
,DelayedArray
,
DelayedMatrixStats
,HDF5Array
,limma
,SummarizedExperiment
-
CRAN 包
assertthat
,ggrastr
,grr
,pbmcapply
,pheatmap
,proxy
,pscl
,
rsample
,RhpcBLASctl
,sf
,slam
,spdep
,speedglm
,viridis
6.1.1 先安装 Bioconductor 包
安装缺失的 Bioconductor 包(一次性安装):
BiocManager::install(c(
"SingleCellExperiment",
"batchelor",
"BiocParallel",
"DelayedArray",
"DelayedMatrixStats",
"HDF5Array",
"limma",
"SummarizedExperiment"
), update = FALSE)
提示:如果安装时提示其他包版本冲突,或需要更新,可以将
update = TRUE
,或按需手动升级冲突的包。
6.1.2 再安装 CRAN 包
从 CRAN 安装剩余依赖:
install.packages(c(
"assertthat",
"ggrastr",
"grr",
"pbmcapply",
"pheatmap",
"proxy",
"pscl",
"rsample",
"RhpcBLASctl",
"sf",
"slam",
"spdep",
"speedglm",
"viridis"
))
安装完重新安装 monocle3时继续报错:
ERROR: dependencies 'leidenbase', 'lmtest', 'proxy', 'RANN', 'RcppAnnoy', 'RcppHNSW', 'Rtsne', 'uwot' are not available for package 'monocle3'
继续安装:
install.packages(c(
"leidenbase",
"lmtest",
"proxy",
"RANN",
"RcppAnnoy",
"RcppHNSW",
"Rtsne",
"uwot"
))
如若网络环境不佳,可尝试选用国内镜像,如:
install.packages("pheatmap", repos="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
或在 Rstudio 的 “Global Options -> Packages -> Primary CRAN repository” 里设置清华或中科大镜像。
6.2. 重新安装 monocle3(源码)
在上述依赖全部安装成功后,再执行从本地源码安装:
install.packages(
"C:/Users/PC/Downloads/monocle3-master",
repos = NULL,
type = "source"
)
注意:
- 如果
monocle3-master
是 ZIP 压缩包,想直接安装,可以:install.packages( "C:/Users/PC/Downloads/monocle3-master.zip", repos = NULL, type = "source" )
- Windows 下若需要编译,必须安装对应版本的 Rtools。安装后重新打开 R/RStudio 才能识别到编译工具。
6.3. 成功后验证加载
library(monocle3)
如若不再出现缺少依赖的错误,说明安装已经成功。
其他可能的问题
- Permission denied:如果在安装到系统库路径时遇到权限问题,可尝试以管理员身份运行 R 或 RStudio,或者把安装路径切换到自己有写权限的文件夹(通过
.libPaths()
设置)。 - 网络问题:如果网络不稳定或者被防火墙限制,可切换到国内镜像或使用离线安装方式(先下载
.tar.gz
/.zip
文件后本地安装)。 - 版本冲突:Bioconductor 包和 CRAN 包之间有时会出现版本不兼容,可在报错时根据提示升级相关依赖,或者回退到兼容的版本。
只要确保所有依赖包正确安装,并有完整的编译环境(尤其在 Windows 上需要 Rtools),就能顺利安装 monocle3。
7. 汇总
-
CRAN:
install.packages("xxx")
- 可指定镜像(
repos
选项),亦可在.Rprofile
中全局设置。
- 可指定镜像(
-
Bioconductor:
BiocManager::install("xxx")
- 先装
BiocManager
,再使用BiocManager
安装相关包。
- 先装
-
GitHub:
- 安装
remotes
或devtools
:install.packages("remotes")
remotes::install_github("user/repo", ref="xxx")
- 安装
-
本地文件:
install.packages("文件名.tar.gz", repos = NULL, type = "source")
- 注意编译环境和操作系统类型(Windows中已编译成二进制的用
win.binary
,其余通常用source
)。
-
安装特定版本:
remotes::install_version("xxx", version="1.2.3")
-
遇到的问题:
- 有一些包要先装上,重新安装即可