How-To: Installing R and R packages using conda

Overview:

R is a programming language used for statistical computing and data analysis, while Conda is a package manager that simplifies software installation and environment management across platforms. You can use Conda to create environments where R can be run, and install R packages within these environments, ensuring a consistent and isolated setup for your projects. This method will also allow you to share your exact custom R environments with others.

Creating a Conda environment with R installed

There are two methods in which you can create a conda environment with your desired version of R, along with any R packages you wish to install. The preferred way (for sake of reproducibility), is by creating an environment.yml file for the conda environment and specifiying the R version and packages you wish to install. The second is to create a new environment with R installed and install packages as you need.

Loading miniconda3:

In order to create a conda environment, we need to have conda available within out session. To do this, simply load miniconda using module load:

$ module load miniconda3/20240410

Once miniconda3 is loaded successfully, choose a method in which to create your environment.

Using an environment.yml:

  1. Creating environment.yml

    1. start by creating a new text file named environment.yml in your project directory

      touch environment.yml
  2. installing R packages

    1. Conda also allows users to be able to create environments for R and allows users to create and manage their own R environments. To do this, you will need to specify a few additional channels for the R libraries. An example environment.yml with the necessary channels and some example packages can be seen below:

      1. name: name-of-your-choosing channels: - defaults - conda-forge - bioconda - R dependencies: - r-base=4.4 # change to desired R version - r-scales - r-seqinr - r-ape - r-castor - r-phytools - and any other additionall packages...

The conda channels defaults and conda-forge are standard conda channels which are, as the first name implies, the default channels. Conda will look here first when trying to satisfy package installations. However, this is not enough to satisfy installing R and its available packages. This is why the bioconda and R channels need to be specified.

 

  1. Creating the conda environment with miniconda3: Once the environment.yml is complete with all required dependencies, we can create the environemnt with the following commands

    Upon successful completion, you can activate the environment by using:

  2. Deactivating conda environment:Once finished with your session, simply enter:

This will quit the conda environment until you reactivate it again when needed.

Not using environment.yml:

  1. Create a conda environment:

    1. To create a conda environment without using an environment.yml file, simply provide a name (“example_environemnt")for your environment in the command below:

      1. Note: This documents focuses on using R you can specify the version of python as seen above with python=VERSION and r=VERSION If not specified, conda will use its default python version.

  2. Activate environment:

    1. Once the environemnt has been created, simply activate it via the command: $ conda activate example_environment (where “example_environment” is the name you specifed in the previous command).

  3. Using R to install packages: Once activated, you have the flexibility to incrementally add new packages and dependencies to your environment as needed to adapt to changing workflow requirements using R’s install.packages() or using conda. This allows you to create a tailor the environment with only the packages you need.

    1. Using install.packages()

    2. Using conda (example r package: ggplot2)

  4. Deactivating conda environment:

    1. Once finished with your session, simply enter:

This will quit the conda environment until you reactivate it again when needed.

Reactivating Existing Conda Environments:

To reactivate an existing environment that was created:

  1. Load miniconda3:

  2. Find environment name:

    1. If you forgot what your environments name was, you can either refer back to your environment.yml file or use the command: $ conda env list. This will produce a list of all the available conda environments and should help you remember what the name was.

  3. Activate environment:

    1. As before, once you have the name of your created environment, simply do: