vlambda博客
学习文章列表

听说你装不了github的包?


我多次介绍github包的使用,但经常会有人反应安装不了。可能网络上404上,不过github的网页总是能够上的,而网页上点击Download ZIP,应该也是可以的。不过啊,很多人安装这个zip有问题,在Windows下,二进制的R包就是zip格式,可能习惯于点击安装,选zip文件吧。但这可不行,因为不是编译好的R包,而是R源码,打成一个压缩包。这两者有点差别。

简单点讲,你平时安装包在CRAN上下载的zip,是动过手脚的,编译好的。而从github上下的zip是没动过手脚的,就是纯代码,压缩一下而已。

所以要安装从github下载的zip包,你需要自己去动手脚,也就是编译一下R包。先解压,然后用R CMD build folder去编译成二进制的包,如果你是windows的话,这时候生成的二进制包,也是zip格式。差别就在这,动手之前和动手之后的。

那么动手之后的,你就可以用R CMD INSTALL或者在R里使用install.packages()去安装了。

稍微还是麻烦点,所以很多人,就是去github下载了zip包,也装不了。

于是我写了一个install.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)

然后呢,这还不够,毕竟这需求多半也是在安装github包时会用到。所以啊,我又写了一个函数,install_zip_gh(),就跟你用devtools::install_github()似的,但是它直接去下载zip包,这样我想大多数人,应该就不会有访问github的问题了吧。下载了zip包之后,它再调用install_zip()给你安装了。

> 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)

这两函数,就存在于yulab-smu/yulab.utils包中,《》也在此包中。

往期精彩