新手的R包安装指南:一条命令就可以把任何来源的R包安装上啦

太长不看版

只需要使用BiocManager::install()就可以把CRANbioconductorGitHub来源的包都安装上了。

R包安装的天下三分

安装R包是每个学习R语言的同学都绕不开的一环。

现在R包主要的来源有三个:

  • CRAN
  • bioconductor
  • GitHub

按照比较经典的方法,三种来源的包应该对应三种安装方式:

  1. CRAN包:

使用install.packages函数安装:

install.packages("ggplot2")
  1. Bioconductor包:

使用BiocManager::install函数安装:

BiocManager::install("GenomicRanges")
  1. GitHub包:

使用devtools安装:

devtools::install_github("hadley/httr")

用错了安装的命令会怎样?

如果你选错了命令,例如用CRAN的安装方式去安装Bioconductor里的GenomicRanges包:

> install.packages("GenomicRanges")

可能会遇到这样的报错:

Warning in install.packages :
  package ‘GenomicRanges’ 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-package

报错“忽悠”你GenomicRanges这个包在当前版本的R不可用。如果你去升级/降级你的R版本,那就算是掉进死胡同里了。

天下三分,合归一统

实际上,BiocManager::install()就可以安装所有三个来源的包了。

CRAN上的包

例如CRAN上的经典包ape:

BiocManager::install("ape")

BiocManager就可以自己到CRAN里去把包装上:

'getOption("repos")' replaces Bioconductor standard repositories, see 'help("repositories", package = "BiocManager")'
for details.
Replacement repositories:
    CRAN: https://cran.wustl.edu/
Bioconductor version 3.17 (BiocManager 1.30.21), R 4.3.1 (2023-06-16 ucrt)
Installing package(s) 'ape'
trying URL 'https://cran.wustl.edu/bin/windows/contrib/4.3/ape_5.7-1.zip'
Content type 'application/zip' length 3405117 bytes (3.2 MB)
downloaded 3.2 MB

package ‘ape’ successfully unpacked and MD5 sums checked

GitHub上的包

包括GitHub上的包,你只要把作者/仓库名写进括号里就可以了。

仓库地址:

https://github.com/r-lib/httr

'getOption("repos")' replaces Bioconductor standard repositories, see 'help("repositories", package = "BiocManager")'
for details.
Replacement repositories:
    CRAN: https://cran.wustl.edu/
Bioconductor version 3.17 (BiocManager 1.30.21), R 4.3.1 (2023-06-16 ucrt)
Installing github package(s) 'r-lib/httr'
Downloading GitHub repo r-lib/httr@HEAD

就装好了。

注意,你得先安装上remotes这个包才能安装GitHub上的包:

BiocManager::install("remotes")

根据这个remotes包的介绍,其实除了GitHub之外的其他类似平台也是可以安装的,例如GitLab和Bitbucket。

Download and install R packages stored in 'GitHub', 'GitLab', 'Bitbucket', 'Bioconductor', or plain 'subversion' or 'git' repositories.

萌哥碎碎念

  1. 最近在学R语言,突然发现R在某些方面比SHELL似乎确实是优雅了不少。用SHELL写可能吭哧吭哧半天成果还是个半成品,R可能咔咔就整完了。
  2. 现在越来越倾向于多种语言混合做数据分析,用Rstudio开一个Rmarkdown,啥python啊R啊SHELL啊都往里面招呼,确实还蛮过瘾的。博采众长嘛。
  3. 以前最怕的是遇到问题查不到结果,现在有GPT了大部分的错误直接把报错贴给GPT它就给你分析出结果了,确实是实实在在地提高了生产力。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容