如何让remotes::install_local()忽略DESCRIPTION中Remotes的远程仓库地址
我有两个R包packageone和packagetwo,其中packagetwo依赖packageone,并在其DESCRIPTION文件中通过Remotes字段指定了packageone的远程仓库地址:
Remotes: git::http://url/to/packageone.git
由于公司政策限制,同事无法访问这两个包的远程仓库,于是我将两个包的仓库打包后让同事用remotes::install_local()本地安装:
remotes::install_local("path/to/packageone", upgrade = "never") remotes::install_local("path/to/packagetwo", upgrade = "never")
但即便packageone已经成功安装,安装packagetwo时仍报错——remotes::install_local()仍会尝试访问DESCRIPTION里指定的远程仓库地址,错误信息如下:
Error: Failed to install 'packagetwo' from local: Command failed (128) In addition: Warning message: In system(full, intern = TRUE, ignore.stderr = quiet) : running command ''/usr/bin/git' ls-remote http://url/to/packageone.git 2>/dev/null' had status 128 and error message 'Function not implemented'
注:此行为是remotes包的有意设计。
需求:在保留packagetwo的CRAN依赖安装功能的前提下,让remotes::install_local()忽略DESCRIPTION中Remotes指定的远程仓库地址。
以下几种方法可解决问题,且均能保留packagetwo的CRAN依赖安装能力:
方法1:临时修改DESCRIPTION文件
安装packagetwo前,临时注释或删除其DESCRIPTION文件中的Remotes字段,安装完成后可按需恢复:
- 打开本地
packagetwo文件夹下的DESCRIPTION文件 - 将Remotes部分注释:
# Remotes: # git::http://url/to/packageone.git
- 执行本地安装:
remotes::install_local("path/to/packagetwo", upgrade = "never")
- (可选)安装完成后恢复原Remotes字段内容。
方法2:关闭依赖检查安装
若已确认packageone及所有CRAN依赖均已安装,可使用devtools的安装命令关闭依赖检查:
devtools::install_local("path/to/packagetwo", dependencies = FALSE, upgrade = "never")
注意:需确保所有依赖(含CRAN包)已提前安装,否则包可能无法正常运行。
方法3:通过环境变量忽略Remotes
利用remotes包支持的环境变量,强制忽略所有Remotes指定的远程依赖,无需修改DESCRIPTION:
- 安装前设置环境变量:
Sys.setenv(REMOTES_IGNORE_REMOTES = "true")
- 执行本地安装:
remotes::install_local("path/to/packagetwo", upgrade = "never")
- (可选)安装完成后清除该环境变量:
Sys.unsetenv("REMOTES_IGNORE_REMOTES")
此方法不会影响CRAN依赖的自动安装,remotes仍会正常处理CRAN上的依赖包。
内容的提问来源于stack exchange,提问作者mikeck

