R 中的虚拟环境? ?

我发现了几篇关于 R 语言的最佳实践、可重复性和工作流程的文章,例如:

一个主要的关注点是确保代码的可移植性,在这个意义上,将代码移动到一台新机器(可能运行不同的操作系统)是相对简单的,并且给出相同的结果。

由于具有 Python 背景,我已经习惯了虚拟环境的概念。当加上一个简单的必需软件包列表时,这在一定程度上可以确保所安装的软件包和库可以在任何机器上使用,而不会引起太大的麻烦。当然,这并不能保证——不同的操作系统有它们自己的缺点和特点——但它可以让你达到95% 的目标。

R 中是否存在这样的东西?即使没那么复杂。例如,仅仅维护一个所需软件包的纯文本列表和一个将安装任何丢失的脚本?

我即将开始第一次认真地使用 R,可能与 Sweave 一起使用,并且希望以尽可能最好的方式开始!谢谢你的建议。

34990 次浏览

I'm going to use the comment posted by @cboettig in order to resolve this question.

Packrat

Packrat is a dependency management system for R. Gives you three important advantages (all of them focused in your portability needs)

  • Isolated : Installing a new or updated package for one project won’t break your other projects, and vice versa. That’s because packrat gives each project its own private package library.

  • Portable: Easily transport your projects from one computer to another, even across different platforms. Packrat makes it easy to install the packages your project depends on.

  • Reproducible: Packrat records the exact package versions you depend on, and ensures those exact versions are the ones that get installed wherever you go.

What's next?

  1. Walkthrough guide: http://rstudio.github.io/packrat/walkthrough.html

  2. Most common commands: http://rstudio.github.io/packrat/commands.html

  3. Using Packrat with RStudio: http://rstudio.github.io/packrat/rstudio.html

  4. Limitations and caveats: http://rstudio.github.io/packrat/limitations.html

Update: Packrat has been soft-deprecated and is now superseded by renv, so you might want to check this package instead.

The Anaconda package manager conda supports creating R environments.

conda create -n r-environment r-essentials r-base
conda activate r-environment

I have had a great experience using conda to maintain different Python installations, both user specific and several versions for the same user. I have tested R with conda and the jupyter-notebook and it works great. At least for my needs, which includes RNA-sequencing analyses using the DEseq2 and related packages, as well as data.table and dplyr. There are many bioconductor packages available in conda via bioconda and according to the comments on this SO question, it seems like install.packages() might work as well.

Check out roveR, the R container management solution. For details, see https://www.slideshare.net/DavidKunFF/ownr-technical-introduction, in particular slide 12.

To install roveR, execute the following command in R:

install.packages("rover", repos = c("https://lair.functionalfinances.com/repos/shared", "https://lair.functionalfinances.com/repos/cran"))

To make full use of the power of roveR (including installing specific versions of packages for reproducibility), you will need access to a laiR - for CRAN, you can use our laiR instance at https://lair.ownr.io, for uploading your own packages and sharing them with your organization you will need a laiR license. You can contact us on the email address in the presentation linked above.

To add to this:

Note: 1. Have Anaconda installed already 2. Assumed your working directory is "C:"

To create desired environment -> "r_environment_name"

C:\>conda create -n "r_environment_name" r-essentials r-base

To see available environments

C:\>conda info --envs

. .. ...

To activate environment

C:\>conda activate "r_environment_name"


(r_environment_name) C:\>

Launch Jupyter Notebook and let the party begins

(r_environment_name) C:\> jupyter notebook

For a similar "requirements.txt", perhaps this link will help -> Is there something like requirements.txt for R?

It looks like there is another option from RStudio devs, renv. It's available on CRAN and supersedes Packrat.

In short, you use renv::init() to initialize your project library, and use renv::snapshot() / renv::restore() to save and load the state of your library.

I prefer this option to conda r-enviroments because here everything is stored in the file renv.lock, which can be committed to a Git repo and distributed to the team.