[英]How do I compile a R package binary directly from Github?
devtools
软件包提供了通过install_github
命令从Github安装软件包的install_github
。
使用devtools
软件包中的build
命令,可以从本地文件夹编译R软件包二进制文件。
是否还可以使用build
从Github文件夹直接编译(而不是安装)R软件包二进制文件,例如build("https://github.com/user/rpackage")
?
有趣的问题。 我没有意识到为此目的而构建的功能,但这并不意味着它是不可能的。 我根据学习了一些研究从devtools
软件包中导出的未导出函数的知识后,为此编写了一个函数,具体来说,
# devtools:::git_remote
# devtools:::remote
# devtools:::install_remotes
# devtools:::try_install_remote
# devtools:::install_remote
# devtools:::install
# devtools:::R
# devtools:::remote_download.git_remote
函数build_from_git
将要求您安装devtools
和git2r
。 此函数将获取git服务器上托管的R包的url,创建一个临时目录,克隆存储库,构建该包,将.tar.gz移至工作目录,然后删除临时文件。
请注意,在这项工作中我经常使用:::
,通常不建议这样做,如Writing R Extensions手册中所述。 但是,当您需要/想要使用另一个软件包中的非导出函数时,这是一种合理的编程方法。 参数与devtools::install_git
的参数相同。
build_from_git <- function(url, subdir = NULL, branch = NULL, credentials = NULL, progress = interactive()) {
grmt <- devtools:::git_remote(url, subdir = subdir, branch = branch, credentials = NULL)
bundle <- "__temp__"
git2r::clone(grmt$url, bundle, credentials = grmt$credentials, progress = progress)
if (!is.null(grmt$branch)) {
r <- git2r::repository(bundle)
git2r::checkout(r, grmt$branch)
}
on.exit(unlink(bundle, recursive = TRUE), add = TRUE)
sourcepkg <- devtools::as.package(devtools:::source_pkg(bundle, subdir = grmt$subdir))
on.exit(unlink(sourcepkg, recursive = TRUE), add = TRUE)
devtools:::R("CMD build . ", path = "__temp__")
system("mv __temp__/*.tar.gz .")
}
使用示例:
build_from_git(url = "https://github.com/dewittpe/qwraps2.git", progress = interactive())
您应该在工作目录中看到.tar.gz
文件。
以上工作的会话信息为:
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.19.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.1 withr_1.0.2 memoise_1.1.0 git2r_0.19.0
[5] digest_0.6.12 devtools_1.13.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.