R (virtual envs)#

For those who utilise and develop R software, we recommend using virtual environments where possible. Virtual environments allow you have multiple different R environments, where you can install and manage whatever packages/libraries you require and they are installed in your own directory. They additionally allow you to load them as and when you need them.

R virtual environments#

Conda envs provide a way to have R virtual environments. It can be accessed via the anaconda3 module.

Note

for information on modules, see Environment modules (Lmod).

Start by loading the anaconda3 module:

[abc123@login7(eureka) ~]$ module load anaconda3

Creating a Conda env#

If its your first time, using conda envs you will to run conda init bash then log out and in back to the terminal. This will add the Conda environment permanently and is indicated by the (base) next to your command prompt.

Then you can create a Conda virtual R environment using the command conda create -n <env name> r-essentials r-base.

These steps are shown in the example below creating a Conda env called my_r_env:

Creating an R conda env called “my_r_env”#
(base) [abc123@login7(eureka) ~]$ conda create -n my_r_env r-essentials r-base
Collecting package metadata (repodata.json): done
Solving environment: done
.....
.....
.....
.....
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate my_r_env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Activating a Conda env#

Once created we can then activate the Conda Env using conda activate <env name>.

Once activated, your command prompt will change to indicate the virtual environment is loaded.

Activating an R conda env called “my_r_env”#
(base) [abc123@login7(eureka) ~]$ conda activate my_r_env
(my_r_env) [abc123@login7(eureka) ~]$

Customising a Conda env#

Once activated you’ll have access to the R and Rscript commands, and you can begin installing any package you wish in your environment using conda. All packages installed when inside the Conda env will be installed to the directory /users/<user name>/.conda in your home directory. For example, we could install the rstudio package to the environment using conda install -c r <package name>, as shown below:

Installing R packages into your conda env#
(my_r_env) [abc123@login7(eureka) ~]$ conda install -c r rstudio
Collecting package metadata (repodata.json): done
Solving environment: done
.....
.....
.....
r-rsconnect-0.8.13   | 550 KB    | ############################################################ | 100%
r-bit-1.1_14         | 243 KB    | ############################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

De-activating a conda env#

To deactivate your Conda env you simply need to use the command conda deactivate. This will unload the environment and it will be indicated by the removal of the name of the Conda env you had loaded from the command prompt.

De-activating your conda env#
(my_r_env) [abc123@login7(eureka) ~]$ conda deactivate
(base) [abc123@login7(eureka) ~]$

More information can be found from Anaconda’s Website on Conda Envs.

Tip

You can create and delete as many Conda R environments as you like, each with their own purpose, versions of packages, libraries and applications.

Note

We will not install any R packages to the system’s R on EUREKA