如何直接从 Github 编译 R 包二进制文件?
How do I compile a R package binary directly from Github?
devtools
包提供了从 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,创建一个临时目录,克隆 repo,构建包,将 .tar.gz 移动到工作目录,然后删除临时文件。
请注意,我在这项工作中经常使用 :::
,Writing R Extensions 手册中通常不推荐这样做。但是,当您 need/want 使用来自另一个包的非导出函数时,这是一种合理的编程方法。参数与 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
devtools
包提供了从 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,创建一个临时目录,克隆 repo,构建包,将 .tar.gz 移动到工作目录,然后删除临时文件。
请注意,我在这项工作中经常使用 :::
,Writing R Extensions 手册中通常不推荐这样做。但是,当您 need/want 使用来自另一个包的非导出函数时,这是一种合理的编程方法。参数与 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