Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install R packages using conda via an environment.yml file

Normally I create conda environments like...

conda env create -f environment.yml
conda activate env_name

Normally I work in Python, where a typical environment.yml simple file might looks like this...

name: env_name
dependencies:
  - python=3.7
  - pip=19.3
  - pandas=0.24.2
  - pip:
    - scipy==1.2.1

What should the environment.yml file look like to install R packages? The packages are on CRAN

like image 700
Chris_Rands Avatar asked Mar 19 '20 11:03

Chris_Rands


People also ask

Can you install R packages with conda?

install r and r-essentials: conda install -c r r r-essentials. activate your new conda environment: conda activate clermonTyping. Open up R in the terminal: R. install whatever packages you need directly in R (in my case it was tidyverse and knitr): install.

What is environment Yml in conda?

yml file. Note that by convention Conda environment files are called environment. yml . As such if you use the conda env create sub-command without passing the --file option, then conda will expect to find a file called environment.


1 Answers

A general rule of thumb is that most R packages have corresponding packages in Anaconda Cloud with the prefix r- added. While the defaults channel covers commonly-used packages, the conda-forge channel has the most thorough coverage of CRAN and has helpful scripts for adding new ones. I would generally recommend prioritizing conda-forge when creating R environments.

For bioinformaticians, all Bioconductor packages are available through the bioconda channel, with a bioconductor- prefix and lowercase. For example, SingleCellExperiment is packaged as bioconductor-singlecellexperiment.

A good place to start is simply searching Anaconda Cloud (example search).

Example

Let's assume you want the tidyverse umbrella package and wish to use R v4.1. A YAML for this would be

name: my_r_env
channels:
 - conda-forge
dependencies:
 - r-base=4.1
 - r-tidyverse

Additional Notes

Avoid using install.packages() from within any R sessions - it is prone to dynamic linking issues due to the R instance's unawareness of compiling inside the environment. This is not an issue for pure R packages, but in that case it should be simple to add the package to conda-forge (takes about 15 mins of work and a ~12-24hr turnaround, IME).

Avoid the RStudio packages from Conda - it is an abandoned project and the old versions are incompatible with newer R versions. This may change once RStudio switches from Qt to Electron. Still, there are better ways to load an environment into RStudio, without having to install the full IDE inside the environment.

like image 156
merv Avatar answered Sep 28 '22 10:09

merv