2016-02-29 138 views
0

devtools包提供了從Github通過install_github命令安裝包的可能性。如何直接從Github編譯R包二進制文件?

使用build命令在devtools包可以從本地文件夾編譯A R包二進制文件。

是否也可以使用build從Github文件夾直接編譯(不安裝)R軟件包二進制文件,即類似build("https://github.com/user/rpackage")

+0

沒有檢查它是否是可能的,但有在下載包並在本地構建它的任何缺點? – nicola

+0

在R內通過單個命令執行它將會更加方便。 – majom

回答

1

有趣的問題。我不知道爲此目的而構建的功能,但這並不意味着這是不可能的。我寫此基礎上我學到了什麼從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手冊中所述。但是,當您需要/想要使用其他包中的非導出函數時,這是一種合理的編程方法。參數與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