在使用R語(yǔ)言進(jìn)行數(shù)據(jù)分析時(shí)天梧,我們時(shí)不時(shí)需要下載github中的R包盔性,但是相信大家經(jīng)常會(huì)遇到一個(gè)問(wèn)題——總是安裝失敗。原因大都因?yàn)榫W(wǎng)絡(luò)原因呢岗,但是github的網(wǎng)頁(yè)總是能夠上的冕香,而網(wǎng)頁(yè)上點(diǎn)擊Download ZIP也是可以的。相信這個(gè)問(wèn)題困擾了不少人后豫。我們經(jīng)常的解決方法是通過(guò)碼云進(jìn)行間接下載悉尾,然后再?gòu)谋镜匕惭b。確實(shí)挫酿,問(wèn)題是解決了构眯,但是步驟繁瑣,對(duì)于追求高效率的同學(xué)來(lái)說(shuō)早龟,這是不可接受的惫霸,正巧今天偶爾看到Y(jié)叔的一個(gè)神包,很好的解決了這個(gè)問(wèn)題葱弟。
廢話不多說(shuō)壹店,開(kāi)始介紹:
相關(guān)函數(shù)存在yulab-smu/yulab.utils包中(https://github.com/YuLab-SMU/yulab.utils)。因?yàn)槲覀冞€沒(méi)有裝這個(gè)包芝加,所以我們先利用碼云將yulab.utils包下載下來(lái)硅卢,接著本地安裝。
yulab.utils包順利安裝完成,可以使用Y叔的那兩個(gè)函數(shù)了--install.zip()函數(shù)和install_zip_gh()函數(shù)
(一)install.zip()函數(shù)
該函數(shù)可以直接根據(jù)github下載得到的壓縮包将塑,進(jìn)行解壓脉顿,編譯和安裝。你只需要給這個(gè)函數(shù)一個(gè)R壓縮包即可点寥。用法也極其簡(jiǎn)單艾疟,就install.zip(“R package.zip”)。
舉例:
library(yulab.utils) #加載yulab.utils包
##需要注意的是工作目錄下必須有g(shù)gtree.zip文件
install_zip("ggtree.zip")
> install_zip("ggtree.zip")
? checking for file ‘/tmp/RtmpHiXVWl/file3ea34246ccb64/ggtree/DESCRIPTION’ ...
─ preparing ‘ggtree’:
? checking DESCRIPTION meta-information ...
─ checking for LF line-endings in source and make files and shell scripts
─ checking for empty or unneeded directories
─ building ‘ggtree_2.1.6.tar.gz’
Installing package into ‘/home/ygc/R/library’
(as ‘lib’ is unspecified)
inferring 'repos = NULL' from 'pkgs'
* installing *source* package ‘ggtree’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggtree)
library(ggtree) #查看ggtree是否安裝成功
(二)install_zip_gh()函數(shù)
這個(gè)函數(shù)類似于devtools::install_github()敢辩,它可以直接去下載zip包汉柒,這樣避免無(wú)法訪問(wèn)github的問(wèn)題。下載好zip包后责鳍,它再調(diào)用install_zip()進(jìn)行編譯安裝。
真的一條命令搞定github R包的安裝兽间。
值得注意的是历葛,install_zip_gh()函數(shù)依賴downloader.R,如果有同學(xué)發(fā)現(xiàn)報(bào)錯(cuò)說(shuō)缺少downloader嘀略,則直接利用 install.packages('downloader')就行了恤溶。
library(yulab.utils)
##這里拿Y叔的ggtree來(lái)演示
install_zip_gh("yulab-smu/ggtree")
> install_zip_gh("yulab-smu/ggtree")
trying URL 'https://codeload.github.com/yulab-smu/ggtree/zip/master'
downloaded 327 KB
checking for file ‘/tmp/RtmpHiXVWl/file3ea34c0f3725/ggtree-master/DESCRIPTION? checking for file ‘/tmp/RtmpHiXVWl/file3ea34c0f3725/ggtree-master/DESCRIPTION’
─ preparing ‘ggtree’:
? checking DESCRIPTION meta-information ...
─ checking for LF line-endings in source and make files and shell scripts
─ checking for empty or unneeded directories
─ building ‘ggtree_2.1.6.tar.gz’
Installing package into ‘/home/ygc/R/library’
(as ‘lib’ is unspecified)
inferring 'repos = NULL' from 'pkgs'
* installing *source* package ‘ggtree’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggtree)
library(ggtree) #查看ggtree是否安裝成功
至此,這兩個(gè)函數(shù)介紹完畢帜羊,大家以后在下載github的包時(shí)咒程,再也不用擔(dān)心啦。