用途:使用 renv 为 R 项目建立并维护可复现的包依赖环境,适用于新项目初始化、增改依赖、在新机器恢复环境、排查 restore 失败以及接入 CI/CD。
主要能力:renv::init() 创建项目本地库、renv.lock 快照并更新 .Rprofile 激活脚本;renv::install() 安装包(含 GitHub 私有包);renv::snapshot() 记录依赖版本;renv::restore() 按 renv.lock 精确还原;renv::update() 更新指定或全部包;renv::status() 检查同步状态;renv::dependencies() 检查实际使用的包。
使用方式:先确认 getwd() 在正确项目目录,再执行 renv::init();安装包后运行 renv::snapshot();新机器执行 renv::restore()。.Rprofile 建议用 if (file.exists("renv/activate.R")) { source("renv/activate.R") } 条件激活。Git 中提交 renv.lock、renv/activate.R、renv/settings.json 和 .Rprofile,忽略 renv/library/、renv/staging/、renv/cache/。CI 可用 r-lib/actions/setup-renv@v2。
限制与注意:私有 GitHub 包需在 .Renviron 设置 GITHUB_PAT;Linux 缺系统依赖需用 apt-get 安装;大包超时可设 options(timeout = 600);无二进制时 renv 从源码编译,需构建工具;Rscript --vanilla 会跳过 .Rprofile 导致 renv 不激活;Bioconductor 包用 renv::install("bioc::PackageName")。资料未提供具体收费、平台支持范围与效果数据。