zl程序教程

您现在的位置是:首页 >  其他

当前栏目

R包来源及安装指定版本R包方法

2023-02-26 10:16:34 时间

全文共4326字,预计阅读时间为11分钟。

说实话,自从之前好好学习了一下入门的R以后,一直没有用过R,很多东西都忘了,还是需要靠做笔记方便日后查阅。

本期讲一下R包的几种来源以及安装指定版本R包的几种方法。

R包来源及安装方法

1. CRAN网站

可以理解为R包的官网,凡是需要通过CRAN下载的R包,都可以通过install.packages("pkg_name")来安装。

2. Bioconductor

里面多是跟生信相关的R包,通过BiocManager::install("pkg_name")来安装。

3. Github

部分作者在写好R包以后还没来得及上传到CRAN上,便可通过其Github进行安装,通过devtools::install_github("用户名/pkg_name")安装。

# 综上
install.packages("pkg_name") # CRAN
BiocManager::install("pkg_name") # Bioconductor
devtools::install_github("用户名/pkg_name") # Github

如果你不知道你需要的包来源哪里,那就三个都试试呗!

举个栗子,如果我现在需要安装edgeR和limma

> install.packages('edgeR')
Installing package into ‘C:/Users/75256/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
Warning in install.packages :
  package ‘edgeR’ is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

显示不太行,我们再试试Bioconductor

> BiocManager::install("edgeR")
'getOption("repos")' replaces Bioconductor standard repositories, see '?repositories' for
details

replacement repositories:
    CRAN: https://mirrors.tuna.tsinghua.edu.cn/CRAN/

Bioconductor version 3.12 (BiocManager 1.30.12), R 4.0.5 (2021-03-31)
Installing package(s) 'edgeR'
试开URL’https://bioconductor.org/packages/3.12/bioc/bin/windows/contrib/4.0/edgeR_3.32.1.zip'
Content type 'application/zip' length 3371192 bytes (3.2 MB)
downloaded 3.2 MB

package ‘edgeR’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
 C:\Users\75256\AppData\Local\Temp\RtmpInt0Qf\downloaded_packages
Installation paths not writeable, unable to update packages
  path: C:/Program Files/R/R-4.0.5/library
  packages:
    boot, class, cluster, KernSmooth, lattice, MASS, Matrix, mgcv, nlme, nnet, spatial,
    survival
Old packages: 'BiocManager', 'bitops', 'blob', 'broom', 'cachem', 'cli', 'colorspace',
  'cpp11', 'curl', 'dplyr', 'fansi', 'flextable', 'formatR', 'gargle', 'ggplotify',
  'ggsignif', 'htmltools', 'htmlwidgets', 'isoband', 'knitr', 'maptools', 'matrixStats',
  'mime', 'officer', 'openssl', 'paletteer', 'pillar', 'pkgload', 'plotrix', 'R6',
  'RcppArmadillo', 'RCurl', 'remotes', 'rlang', 'rmarkdown', 'roxygen2', 'RSQLite',
  'stringi', 'tibble', 'tinytex', 'utf8', 'XML'
Update all/some/none? [a/s/n]: 
n

成功安装,同理我们发现limma也可以通过Bioconductor安装。

安装指定版本R包

复现文章或R包的更新与当前的R版本或R代码不兼容时,就需要考虑安装某个特定版本的R包了,这里列一些常见的安装指定版本R包的方法供大家参考。

1. devtools安装

install.packages('devtools')
library(devtools)
install_version("edgeR", version = "3.14.0",repos = "http://cran.us.r-project.org") # 可以不加repos

举个栗子,我现在要装Y叔的clusterProfiler,采用如下命令并报错

> BiocManager::install("clusterProfiler")
Bioconductor version 3.12 (BiocManager 1.30.16), R 4.0.3 (2020-10-10)
Installing package(s) 'clusterProfiler'
trying URL 'https://mirrors.ustc.edu.cn/bioc//packages/3.12/bioc/src/contrib/clusterProfiler_3.18.1.tar.gz'
Content type 'application/octet-stream' length 2634922 bytes (2.5 MB)
==================================================
downloaded 2.5 MB

* installing *source* package ‘clusterProfiler’ ...
** using staged installation
** R
** data
** inst
** byte-compile and prepare package for lazy loading
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
  object 'get_fun_from_pkg' not found
Error: unable to load R code in package ‘clusterProfiler’
Execution halted
ERROR: lazy loading failed for package ‘clusterProfiler’
* removing ‘/home/data/xiaowang/R/x86_64-pc-linux-gnu-library/4.0/clusterProfiler’

The downloaded source packages are in
 ‘/tmp/RtmpSczSko/downloaded_packages’
Installation paths not writeable, unable to update packages
  path: /usr/local/lib/R/library
  packages:
    boot, class, cluster, codetools, foreign, KernSmooth, lattice, MASS, Matrix, mgcv, nlme, nnet, spatial, survival
Warning message:
In .inet_warning(msg) :
  installation of package ‘clusterProfiler’ had non-zero exit status

仔细看,关键报错信息为

Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
  object 'get_fun_from_pkg' not found

经过查询发现(参考资料2),clusterProfiler的依赖包rvcheck的版本太新,下载0.1.8的版本即可。

在装有devtools的基础上,我使用以下命令重新装了0.1.8版本的rvcheck后,顺利的安装了clusterProfiler。

install_version("rvcheck",version = "0.1.8")

需要注意的是devtools这个方法并不适用所有的R包。

2. 源码安装

在这里也许能找到你需要的R包历史版本。

https://cran.r-project.org/src/contrib/Archive/

packageurl <- "https://cran.r-project.org/src/contrib/Archive/limma/limma_1.8.10.tar.gz" # 安装1.8.10版本的limma
install.packages(packageurl, repos=NULL, type="source")

3. CMD安装

这种方法需要依赖Rtools

wget https://cran.r-project.org/src/contrib/Archive/limma/limma_1.8.10.tar.gz
R CMD INSTALL limma_1.8.10.tar.gz

但其实,在实际操作时我发现能不能装上还得看是什么R包以及你的网络情况

最后,对于安装软件,知道方法是基础,理解error信息更关键

参考资料

  1. https://www.jianshu.com/p/163f77ecfcfb
  2. https://www.jianshu.com/p/71fab5764d3a