Skip to content

云生信镜像使用教程 ​

一、关于镜像 ​

关于短别名

/cran、/bioconductor、/conda/... 与上面的完整路径等价,本文统一使用完整路径。无论使用哪种写法,GitHub R 包的 host 都必须写成 http://iyun70.com:18011/softs/Rpackages。

先确认镜像可以访问,下面的命令应返回 HTTP 200:

bash
curl -I --max-time 20 "http://iyun70.com:18011/softs/Rpackages/cran/src/contrib/PACKAGES"
curl -I --max-time 20 "http://iyun70.com:18011/softs/Rpackages/bioconductor/packages/release/bioc/src/contrib/PACKAGES.gz"

返回 200 后再安装。如果 R 报“无法读取 PACKAGES”或“package is not available”,先检查主机地址、端口、DNS 和网络,不要把另一台机器的 127.0.0.1 当成线上镜像。

二、R 包镜像 ​

2.1 一键配置(推荐) ​

镜像站提供了一个会话级配置脚本,只对当前 R 会话生效:

r
source("http://iyun70.com:18011/softs/Rpackages/setup.R")

脚本会一次性设置 repos、BioC_mirror、BIOCONDUCTOR_CONFIG_FILE 和 remotes 专用的 R_BIOC_CONFIG_URL,并用 trace() 改写 remotes 内部的 build_url,把 api.github.com 的调用(包括 dependencies = TRUE 时依赖包的 tarball 下载、ref 到 commit SHA 的解析和 DESCRIPTION 读取)统一路由到镜像。

因此加载脚本后,2.5 中的 host、R_BIOC_CONFIG_URL 和 BioC_mirror 都可以省略;未加载脚本时才需要按下文手动配置。

脚本依赖 remotes。如果尚未安装,先执行:

r
if (!requireNamespace("remotes", quietly = TRUE)) {
  install.packages("remotes", repos = "http://iyun70.com:18011/softs/Rpackages/cran")
}

2.2 安装 CRAN 包 ​

r
options(
  repos = c(CRAN = "http://iyun70.com:18011/softs/Rpackages/cran"),
  timeout = 3600
)

安装单个包:

r
install.packages("janitor")

安装多个包:

r
install.packages(
  c("janitor", "SCpubr", "scTenifoldKnk"),
  dependencies = NA
)

安装完成后检查:

r
library(janitor)
packageVersion("janitor")

2.3 安装指定版本的 CRAN 包 ​

remotes::install_version() 会从 CRAN 的 src/contrib/Archive/ 目录取历史版本:

r
remotes::install_version(
  "igraph",
  version = "1.2.11",
  repos = "http://iyun70.com:18011/softs/Rpackages/cran",
  upgrade = "never",
  dependencies = FALSE
)

镜像会按完整的 Archive 文件路径回源并缓存,重复安装同一个版本会命中缓存(响应头 X-Mirror-Cache: HIT)。依赖仍由 remotes/R 客户端解析,继续从配置好的 CRAN、Bioconductor 镜像获取。

2.4 安装 Bioconductor 包 ​

BioC_mirror 只负责包仓库,R 与 Bioconductor 的版本映射还需要指定 config.yaml。两者要一起设置:

r
options(
  repos = c(CRAN = "http://iyun70.com:18011/softs/Rpackages/cran"),
  BioC_mirror = "http://iyun70.com:18011/softs/Rpackages/bioconductor",
  timeout = 3600
)

Sys.setenv(
  BIOCONDUCTOR_CONFIG_FILE =
    "http://iyun70.com:18011/softs/Rpackages/bioconductor/config.yaml"
)

if (!requireNamespace("BiocManager", quietly = TRUE)) {
  install.packages("BiocManager")
}

BiocManager::version()
BiocManager::install("BiocGenerics", ask = FALSE, update = FALSE)

把 BiocGenerics 换成需要安装的包名,例如:

r
BiocManager::install("DESeq2", ask = FALSE, update = FALSE)

检查:

r
library(BiocGenerics)
packageVersion("BiocGenerics")

几点说明:

  • Bioconductor 镜像包含 bioc、data/annotation 和 data/experiment 三类仓库,BioC_mirror 不要配到 CRAN 路径。
  • BIOCONDUCTOR_CONFIG_FILE 是 BiocManager 使用的版本映射;remotes 使用的是独立的 R_BIOC_CONFIG_URL(见 2.5),两者互不相通,只配前者解决不了 remotes 的 config.yaml 报错。
  • install.packages() 安装 CRAN 包不经过 BiocManager 版本校验,dependencies = FALSE 也不会跳过 BiocManager 自身的版本检查;不要把“部分包能装”当成版本映射已经正常。
  • 显式指定 version= 不是版本映射不可用时的通用解决办法,应先确认 config.yaml 可读、镜像有对应版本索引。

2.5 安装 GitHub R 包 ​

先确认已安装 remotes(或 devtools):

r
if (!requireNamespace("remotes", quietly = TRUE)) {
  install.packages("remotes", repos = "http://iyun70.com:18011/softs/Rpackages/cran")
}

通过镜像安装 GitHub 包,host 固定为 http://iyun70.com:18011/softs/Rpackages:

r
github_host <- "http://iyun70.com:18011/softs/Rpackages"

remotes::install_github(
  "junjunlab/GseaVis",
  host = github_host,
  upgrade = "never",
  dependencies = FALSE
)

devtools::install_github() 同样支持 host 参数。仓库名称格式为 作者/仓库名,例如:

text
junjunlab/GseaVis
sfirke/janitor

检查:

r
library(GseaVis)
packageVersion("GseaVis")

使用 dependencies = TRUE 时,remotes 还需要读取 Bioconductor 版本映射,否则会去 bioconductor.org 拉取 config.yaml。先设置好再安装:

r
Sys.setenv(
  R_BIOC_CONFIG_URL =
    "http://iyun70.com:18011/softs/Rpackages/bioconductor/config.yaml"
)
options(
  repos = c(CRAN = "http://iyun70.com:18011/softs/Rpackages/cran"),
  BioC_mirror = "http://iyun70.com:18011/softs/Rpackages/bioconductor",
  timeout = 3600
)

remotes::install_github(
  "immunogenomics/presto",
  host = github_host,
  upgrade = "never",
  dependencies = TRUE
)

host 不要写成 github-api

host 必须是 http://iyun70.com:18011/softs/Rpackages,不要写成 .../Rpackages/github-api。

  • .../Rpackages(正确):命中镜像自有的 /repos/{owner}/{repo}/tarball/{ref} 处理器,先完整回源缓存 tarball,再 302 到同主机的 /github/... 静态路径,全程不依赖外网。
  • .../Rpackages/github-api(错误):nginx 会把请求裸代理到真实的 api.github.com,其 tarball 响应会 302 跳到 codeload.github.com,客户端直连该域名通常会失败。github-api/ 只用于 REST API 查询。

安装指定分支、tag 或 commit:

r
remotes::install_github(
  "owner/repository",
  ref = "dev", # 也可以填 tag(如 "v1.2.3")或完整 commit SHA
  host = github_host,
  dependencies = FALSE
)

ref 填完整 commit SHA 时会跳过 ref 解析,直接复用对应的 tarball 缓存。镜像端会缓存 GitHub tarball 以及 ref 到 commit SHA 的解析结果;客户端不需要设置 GitHub PAT,PAT 由镜像端维护。

批量安装示例:

r
github_pkgs <- c(
  "mojaveazure/seurat-disk",
  "immunogenomics/presto",
  "bmbolstad/preprocessCore",
  "bhklab/survcomp"
)

for (repo in github_pkgs) {
  message("Installing ", repo)
  remotes::install_github(
    repo,
    host = github_host,
    dependencies = TRUE,
    upgrade = "never"
  )
}

如果出现依赖解析错误,先查看最内层的错误:可能是 GitHub API/上游不可达、R 版本不满足、系统编译器缺失,或某个依赖包安装失败。

2.6 安装到指定目录 ​

r
dir.create("~/R/library", recursive = TRUE, showWarnings = FALSE)

install.packages(
  "janitor",
  lib = "~/R/library",
  repos = c(CRAN = "http://iyun70.com:18011/softs/Rpackages/cran")
)

.libPaths(c("~/R/library", .libPaths()))
library(janitor)

2.7 查看镜像中是否有某个包 ​

r
available <- available.packages(
  repos = "http://iyun70.com:18011/softs/Rpackages/cran"
)

available["janitor", "Version"]

能显示版本号,就可以继续安装。

2.8 带依赖的复杂包与源码编译 ​

复杂包安装不是一次下载,而是多个依赖包连续请求同一组镜像地址:首次请求通常是 MISS,后续相同版本会复用 HIT。

r
options(
  repos = c(CRAN = "http://iyun70.com:18011/softs/Rpackages/cran"),
  BioC_mirror = "http://iyun70.com:18011/softs/Rpackages/bioconductor",
  timeout = 3600
)

install.packages(
  c("spam", "RSpectra", "igraph", "irlba", "sctransform", "Rtsne", "Rcpp"),
  dependencies = TRUE,
  update = FALSE
)
  • 不要让多个独立 R 进程同时写同一个 library,否则可能产生 00LOCK 目录冲突。
  • 镜像主要提供源码包缓存,不承诺 Linux/Windows 二进制包。
  • 需要编译的包(如依赖 CMake、Fortran、HDF5、LAPACK 或系统动态库)要由服务器系统提供对应的开发环境和运行库;镜像不代替系统依赖。
  • “下载成功”和“编译安装成功”是两个独立结果。

2.9 关于 R-Forge ​

镜像缓存的是 CRAN、Bioconductor、GitHub 和 Conda 的文件,不包含 R-Forge。需要 R-Forge 上的包时,先在运行 R 的同一台机器检查上游:

bash
curl -I --max-time 20 http://R-Forge.R-project.org/src/contrib/PACKAGES
curl -I --max-time 20 https://R-Forge.R-project.org/src/contrib/PACKAGES

当前 iyun70 实测 R-Forge 的 HTTP/HTTPS 上游均无法建立连接,经代理访问也会出现 TLS EOF,因此暂不将该上游接入镜像缓存。只有在上述检查返回 HTTP 200 后,才适合进一步接入;如果包已经迁移到 GitHub,请改用 2.5 的方式安装。

不要把 R-Forge.R-project.org 直接替换成 CRAN 路径:两者仓库索引和包来源不同,镜像没有上游可回源时不能凭空生成这些包。

三、Conda 软件包镜像 ​

3.1 配置镜像频道 ​

在服务器终端执行:

bash
conda config --prepend channels http://iyun70.com:18011/softs/Rpackages/conda/conda-forge
conda config --prepend channels http://iyun70.com:18011/softs/Rpackages/conda/bioconda
conda config --set show_channel_urls yes

需要使用 defaults(Anaconda 官方频道)时,再执行:

bash
conda config --add channels http://iyun70.com:18011/softs/Rpackages/conda/pkgs/main
conda config --add channels http://iyun70.com:18011/softs/Rpackages/conda/pkgs/r

短别名 /conda/conda-forge、/conda/bioconda 等与上面的完整路径等价。

3.2 创建环境并安装软件 ​

bash
conda create -n bio \
  -c http://iyun70.com:18011/softs/Rpackages/conda/conda-forge \
  -c http://iyun70.com:18011/softs/Rpackages/conda/bioconda \
  r-base=4.4

conda activate bio
conda install -c http://iyun70.com:18011/softs/Rpackages/conda/conda-forge r-janitor

把 r-janitor 换成需要安装的软件包名称即可。

3.3 查看当前频道 ​

bash
conda config --show channels

3.4 清理默认频道(可选) ​

如果配置后仍然访问官方地址,检查 ~/.condarc,只保留镜像地址即可,例如:

yaml
channels:
  - http://iyun70.com:18011/softs/Rpackages/conda/conda-forge
  - http://iyun70.com:18011/softs/Rpackages/conda/bioconda
  - http://iyun70.com:18011/softs/Rpackages/conda/pkgs/main
  - http://iyun70.com:18011/softs/Rpackages/conda/pkgs/r
show_channel_urls: true

四、常见问题 ​

现象处理方法
R 提示 cannot open URL .../src/contrib/PACKAGES检查 repos 是否为 http://iyun70.com:18011/softs/Rpackages/cran,不要填写站点根地址。
安装中途超时在 R 中执行 options(timeout = 3600),然后重新安装。
Bioconductor 提示版本无法验证同时设置 BioC_mirror 和 BIOCONDUCTOR_CONFIG_FILE,用 BiocManager::version() 确认映射可用。
GitHub 包提示 403 或 rate limit检查仓库名称和版本;先安装缺少的 CRAN 依赖,稍后再试。
GitHub 包提示 404 或 502检查 owner/repository、ref 或 commit SHA 是否填写正确。
GitHub 包提示 cannot open URL .../bioconductor.org/config.yaml设置 R_BIOC_CONFIG_URL(不是 BIOCONDUCTOR_CONFIG_FILE),并确认 host 为 .../softs/Rpackages。
源码包编译失败或提示缺少头文件补齐系统开发库和编译器;镜像只负责下载,不代替系统依赖。
出现 00LOCK 目录先停止其他安装任务,确认没有 R 进程正在安装,再删除对应的 00LOCK-* 目录并重试。
Conda 仍访问官方频道用 conda config --show channels 确认,或清理 ~/.condarc 中的旧频道。
首次安装较慢镜像按需回源,第一次请求需要等待下载,缓存后再次安装会更快。
8011 与 18011 的关系8011 是另一个静态服务,与 18011 网关不是同一入口,以实际路由部署为准。

安装时请一次只执行一个任务

不要同时在多个 R 会话中安装包,也不要同时修改同一个 Conda 环境。

五、一页速查 ​

R / Bioconductor ​

r
mirror <- "http://iyun70.com:18011/softs/Rpackages"

options(
  repos = c(CRAN = paste0(mirror, "/cran")),
  BioC_mirror = paste0(mirror, "/bioconductor"),
  timeout = 3600
)

Sys.setenv(
  BIOCONDUCTOR_CONFIG_FILE =
    paste0(mirror, "/bioconductor/config.yaml")
)

GitHub R 包 ​

r
remotes::install_github(
  "owner/repository",
  host = "http://iyun70.com:18011/softs/Rpackages",
  dependencies = FALSE
)

一键配置 ​

r
source("http://iyun70.com:18011/softs/Rpackages/setup.R")

Conda ​

bash
conda config --prepend channels http://iyun70.com:18011/softs/Rpackages/conda/conda-forge
conda config --prepend channels http://iyun70.com:18011/softs/Rpackages/conda/bioconda