简体   繁体   中英

How do I compile a R package binary directly from Github?

The devtools package offers the possiblity to install a package right from Github through the install_github command.

Using the build command in the devtools package one can compile a R package binary from a local folder.

Is it also possible to use build to directly compile (not install) a R package binary from a Github folder, ie something like build("https://github.com/user/rpackage") ?

Interesting question. I'm unaware of a function that has been built for this purpose but that doesn't mean it is not possible. I wrote a function for this based on what I learned for studying several non-exported functions from the devtools package, specifically,

# devtools:::git_remote
# devtools:::remote
# devtools:::install_remotes
# devtools:::try_install_remote
# devtools:::install_remote
# devtools:::install
# devtools:::R 
# devtools:::remote_download.git_remote

The function build_from_git will require that you have devtools and git2r installed. This function will take the url for a R package hosted on a git server, create a temp directory, clone the repo, build the package, move the .tar.gz to the working directory, and then delete the temporary files.

Note that I'm using ::: often in this work, which is generally not recommended as noted in the Writing R Extensions manual. However, when you need/want to use non-exported functions from another package this is a reasonable programming approach. The arguments are the same as from 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 .")
}

Example use:

build_from_git(url = "https://github.com/dewittpe/qwraps2.git", progress = interactive())

You should see the .tar.gz file in your working directory.

The session info for the work done above is:

> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM