Skip to content

Instantly share code, notes, and snippets.

@bakerwm
Created September 18, 2019 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakerwm/5d2dd2fb955b0aad5bd224fc769f2cf5 to your computer and use it in GitHub Desktop.
Save bakerwm/5d2dd2fb955b0aad5bd224fc769f2cf5 to your computer and use it in GitHub Desktop.
20190910-manager_R_packages
title date draft categories tags keywords
Manage R packages
2019-09-10 16:59:04 +0800
false
Linux
basic
R
update
linux

本文涉及到 R 和 R packages 的安装、更新等。

工作常用 Linux 环境,偶尔会遇到 R版本更新,R packages 更新,等问题;

把我的日常使用中遇到的问题整理如下:

  • 安装 RLinux / Windows
  • 安装 R packages: CRAN, Bioconductor, Github
  • 管理 library()
  • 升级 R

1. 安装 R

Ubuntu 18.04 server 系统为例,需要在系统中增加专门的 R repository,以下操作需要用到 sudo权限;

# Add GPG key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

# Add the R repository
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'

# Update your Package list
sudo apt update

# Install R
sudo apt install r-base

# Test
R

例如:

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

If you’re not using 18.04, find the relevant repository from the R Project Ubuntu list, named for each release.

没有root权限怎么办?

参考 conda的解决方案,管理各种软件的版本;

2. 安装R包

常用的R软件包有几个来源:

  • a. 来自CRAN的软件包
install.packages("Name-of-package")

Bioconductor是一个基于R语言的生物信息学平台,发布生物数据的注释、分析、可视化等软件包。安装相关软件的办法如下:

b. Bioconductor的稳定版软件发布周期是6个月。

# 首先需要安装 bioconductor 团队开发并发布在 CRAN 上的软件包: BiocManager
install.packages("BiocManager")

# 在 bioconductor 网站找到自己需要的软件包,例如:GenomicRanges
BiocManager::install("GenomicRanges")

还有一些 R packages的开发者没有将代码上传到 CRANBioconductor,而是直接发布在 Github上;我们还是有办法安装这些软件包,不过需要先安装一个CRAN上的软件包: devtools

if (!require("devtools"))
    install.packages("devtools")

devtools::install_github()

危险

这么多安装办法,记不住怎么办?当然有办法解决(偷懒),ygc大神介绍,可以通过install.packages()一招搞定 CRANBioconductor两个来源的软件吧;

这是有前提的:使用最新版的 R

你要做的很简单,在~/.Rprofile里加入以下两行:

options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor") utils::setRepositories(ind=1:2)

第一行,使用国内的镜像,我这里用的是清华大学的,第二行,设定install.packages从CRAN和Bioconductor中搜索包,其实你还可以让它支持比如R-Forge以及各种第三方的仓库。

然后你就可以愉快地使用install.packages来安装Bioconductor包了。

请确保你一直在使用最新版本的R。

3. 管理 library()

R默认安装路径是:/usr/bin/R;相应的library路径是:/usr/lib/R/library/ (也许还有其他的路径);

日常使用中,默认的 library 是不可写(需要 root权限);解决办法是设置个人目录下的 library path :

~/.Rprofile文件中添加一行:

.libPaths("~/Library/R/3.5/library")

4. 升级 R

经常遇到需要升级R,接着就面临 R packages更新的问题(通常这个过程会比较头疼);

解决办法如下:

  • a. 使用 步骤1 中提到的办法,安装最新版R (例如: 3.5 to 3.6)
  • b. 默认使用了步骤3的办法管理 library: ~/Library/R/3.5/library
  • c. 修改目录名:~/Library/R/3.5 -> ~/Library/R/3.6
  • d. 修改 ~/.Rprofile 文件中 .libPath()中的目录名
  • e. 更新 packages : rvcheck::update_all(),可以更新 CRAN, BioconductorGithub 包。
  • f. 更新 Bioconductor

某些情况下,R的版本更新和 Bioconductor的更新凑到一起了,顺便也把 Bioconductor也更新了。我这次遇到的情况是,R3.5升级到3.6,这期间,Bioconductor也从3.8升级到3.9

# 前提是已经安装过 bioconductor 3.8
# 使用如下命令,安装最新版 bioconductor
BiocManager::install(version = "3.9")

总结

更新 RR Packages:

  1. 升级 R (参考 步骤1
  2. 修改 ~/.Rprofile 中的 library path
  3. 升级软件包:rvcheck::update_all()
  4. 升级Bioconductor: BiocManager::update(version = "3.9") # 指定当前最新版本号
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment