R包的管理(转自网上)


Rstudio包管理一网打尽,最全的R包管理终极教程

img

学着放下关注

2021.02.02 22:17:30字数 1,871阅读 42

Rstudio/R的包(package)管理是个老生常谈的问题,很少有人去总结,因为大家都认为比较简单。今天我就把所有的安装和管理R包的方法一起写一写。

一、包在哪里?

CRAN

官方包CRAN,通常国内选择镜像,方法自行百度。

Bioconductor

Bioconductor,这个通常只有生物信息学的人才用得到。

Github

Github,不用多说,一般不太稳定,但是想尝鲜可以的。大部分CRAN和Bioconductor都是托管在Github上的。

二、自带安装方法(CRAN)

Install Packages from Repositories or Local Files

e.g.
install.packages("tidyverse")
必须要加引号。该命令默认安装的都是CRAN上的包
默认的包管理,其实完全可以不用命令,因为Rstudio已经给大家带了界面,完全可以点击几下就安装、删除、升级等操作,非常方便。

img

image.png

如果你还是觉得命令行才够装逼,那就用这个命令好了。不过这个命令,还有很多细节,
比如,默认情况下你输入包名,比如说”tidyverse”, 会自动从CRAN上检索对应的包,然后进行下载。如果你希望指定安装本地包,或者一个具体的网络地址,参考代码如下:

# from url resource
install.packages("https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/3.5/Matrix_1.2-15.zip", repos=NULL)
# from local
install.packages("~/../Desktop/Matrix_1.2-15.zip", repos = NULL)

一次安装多个包

# multiple install
install.packages(c("slidify", "tidyverse", "devtools") )

大家还可以参考这篇文章R包安装函数”install.packages”函数详解

help

更多详细操作,请用help

# help
?install.packages()
# OR
help(package = "package")

查看已安装的R包(这个貌似没什么用):installed.packages()
查看某个包的具体描述:packageDescription("tidyverse")

Vignette

Vignette,这个中文意思大概就是使用场景展示,我觉得比help更有用,因为通常都是开发者用实例说明,对于一个包的上手,特别有用。Vignette的方法有两种:

弹出网页浏览

browseVignettes(package="tidyverse")

img

image.png

在命令行浏览

这个方法并不推荐,因为要分2步

  1. 先浏览一下这个package里面到底有几个Vignettes vignette(package = "rvest")
    这时候会弹出一个页面,在里面显示这个包到底有几个Vignettes

    img

    image.png

  2. 继续运行命令,找到你想浏览的那个vignette("harvesting-the-web "),在右侧help面板就会弹出Vignette。

    img

    image.png

How To Update, Remove And Check Installed Packages

#You can check what packages need an update with a call to the function:
old.packages()
#You can update all packages by using:
update.packages()
# Remove the packages
remove.packages("vioplot")

三、Bioconductor

是的!为了Bioconductor的包安装,Bioconductor单独做了一个用来安装的包程序。在R 3.5版本以前还有个方法,但现在R已经4.0了,不再推荐以前的方法。详见
https://www.jianshu.com/p/8e0dece51757
https://bioconductor.org/install/
现在的方法很简单,首先安装BiocManager包,然后就可以通过BiocManager下面的install命令来安装了。

install.packages("BiocManager")     
BiocManager::install("GEOquery") 

这里我特意解释一下**::**在R里面的究竟如何使用,是干什么用的。因为我们后面还会出现。

我们知道,在R中,要想使用某个包下的命令,必须先加载(library),否则系统会提示无该命令(function)。但我们有时候只用一下这个包的命令,没必要还先去加载它,因为加载的包越多,系统负担越重,越慢。所以,就有了**::**,它让我们不必加载这个包,就可以用它里面的命令。比如BiocManager::install("GEOquery")意思是用BiocManager抱里面的install命令来安装这个包。

四、devtools

devtools是个全能型选手,你几乎可以安装任何源头的包,首先是安装它install.packages("devtools")。在windows系统里,使用devtools必须先安装R配套的一个工具,叫做 Rtools on Windows,很多包都依赖这个工具,所以建议直接安装。并且要加入到你的环境变量,默认它在安装的时候会加入。不懂请百度“环境变量”。
下面就是这位全能型选手的命令:

  • install_bioc() from Bioconductor,
  • install_bitbucket() from Bitbucket,
  • install_cran() from CRAN,
  • install_git() from a git repository,
  • install_github() from GitHub,
  • install_local() from a local file,
  • install_svn() from a SVN repository,
  • install_url() from a URL, and
  • install_version() from a specific version of a CRAN package.
    当然一般要在前面加 devtools::
if (!require(devtools)) install.packages("devtools")
devtools::install_github("boxuancui/DataExplorer")

更详细的说明请见help文件或vignette

五、 remotes

remotes包与devtools基本相同,用法也基本相同,官方是这样定义它的:
This package is a lightweight replacement of the install_* functions in devtools. Indeed most of the code was copied over from devtools.
所以,我们强烈推荐用remotes取代devtools,因为它更轻,更小,更好用!而且支持所有的安装源,包括bioconductor
这里我就直接copy remotes的readme文件了。

Currently supported remote types

  • GitHub repositories via install_github.
    remotes::install_github("gaborcsardi/pkgconfig@v2.0.0")
  • Bitbucket repositories via install_bitbucket.
  • Generic git repositories via install_git. They need either a system git installation, or the git2r R package.
  • Local directories or package archive files via install_local.
  • Remote package archive files via install_url.
  • Packages in subversion repositories via install_svn. They need a system subversion installation.
  • Specific package versions from CRAN or other CRAN-like repositories via install_version. This includes outdated and archived packages as well.
  • All dependencies of a package in a local directory via install_deps.

Usage

# GitLab
Remotes: gitlab::jimhester/covr
# Git
Remotes: git::git@bitbucket.org:djnavarro/lsr.git
# Bitbucket
Remotes: bitbucket::sulab/mygene.r@default, djnavarro/lsr
# Bioconductor
Remotes: bioc::3.3/SummarizedExperiment#117513, bioc::release/Biobase
# SVN
Remotes: svn::https://github.com/tidyverse/stringr
# URL
Remotes: url::https://github.com/tidyverse/stringr/archive/HEAD.zip
# Local
Remotes: local::/pkgs/testthat

六、pacman

这个包,才是我们今天的王者,作者是Tyler Rinker,一位能与Hadley Wickham平起平坐大牛。pacman与另外一个包管理工具同名,在Linux 的Arch包管理和Manjaro都是用的pacman管理包。二者都叫pacman,但应该是不同的。
pacman之所好,是因为它简单,不需要那讨厌的“”,很多是连贯操作,而且命令也很短。而且它囊括了前面所有安装包的功能。

pacman依赖devtools,在使用前请安装并library

下面是一些常见的参考命令。

Installing, Loading, Unloading, Updating, & Deleting Packages with pacman

Quick Reference Table

pacman Function Base Equivalent Description
p_load install.packages + library Load and Install Packages
p_install install.packages Install Packages from CRAN
p_load_gh NONE Load and Install GitHub Packages
p_install_gh NONE Install Packages from GitHub
p_install_version install.packages & packageVersion Install Minimum Version of Packages
p_temp NONE Install a Package Temporarily
p_unload detach Unload Packages from the Search Path
p_update update.packages Update Out-of-Date Packages

一个命令,包含安装和加载,十分简单。p_load也是这里面最常用的命令
pacman::p_load(XML, devtools, RCurl, fakePackage, SPSSemulate)
除此之外,对上面的命令甚至还有很多简写,一样执行

Functions for installing, loading, unloading, updating, & deleting packages


Author: Song Pengfei
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Song Pengfei !
评论
  TOC