Using a customized R environment in Anaconda+RStudio

If you need a special-purpose R environment above and beyond what is already available on ASU supercmoputers, you can direct the webapp to use a customized environment from your $HOME directory.

Create the Anaconda Environment

In order for the webapp to load your environment, you must first create the environment from the terminal and name the environment ood. This can be accomplished with the following commands:

$ module load anaconda/py3 $ conda create -n ood -c conda-forge r=4.1.0 [snip] $ source activate ood (ood) $

So long as the anaconda environment is named ood, you can add any packages, using any means you are most familiar with: install.packages(), conda install r-data.table, etc.

Populate the environment with packages

Anaconda environments offer numerous ways to add packages for use within R. The following examples demonstrate how to install the package data-table.

install.packages()

(ood) $ R R version 4.1.0 (2021-05-18) -- "Camp Pontanezen" Copyright (C) 2021 The R Foundation for Statistical Computing Platform: x86_64-conda-linux-gnu (64-bit) [snip] > install.packages('data.table') [snip] > quit() Save workspace image? [y/n/c]: n (ood) $

conda install

$ module load anaconda/py3 $ source activate ood (ood) $ conda search r- [snip] (ood) $ conda install r-data.table

Start the Session

In the webapp, create a new interactive session, selecting RStudio from the server list. At the bottom, choose the R version $HOME/conda/.envs/ood

 

The session will start, and when you connect to it, you’ll see your specific instance of R loaded and ready-to-go:

 

It is also now possible to install.packages() directly from webapp’s console for R, and all installed packages will be persistent in the environment.

What if I already have an environment created?

If you have an existing, operable environment already, you can simply symlink it:

Why isn’t it showing the correct R version?

If ~/.conda/envs/ood does not contain a valid anaconda environment, it will fall back to the system-installed R. The console will report it has loaded the following R 3.6.1 version. It is important to end this R session and start a new one after correcting any issues, as this default environment almost certainly will lack all the packages you need.

To test if the ood environment is missing or invalid, you can type the following from a terminal session:

 

Installing R packages to user directory using CRAN

R will attempt to install to a default directory located inside the R install path. This path is not writable by users and may cause user-level package installs to fail. Installing packages to the user home directory allows users to maintain their own R packages

Create a new directory

This helps keep things organized in the user home directory

 

Load latest version of R available on server. To check the latest version on server

A list of available module will pop up as shown below. You are able to see all versions of R available R on server.

 

For this tutorial, we will use R/4.1.0 To load R and run R console on Agave, enter:

Start R

Below starts the R console

 

Install R package

For this example we will be installing “rmeta” package

Apply function “install.packages” in the R console to download and install desired package.

There are three required parameters for “install.packages” function.

  1. Package name, “remeta” in this case.

  2. URL for the repository where we can obtain the package. Most R packages including “rmeta” are available on “http://cran.r-project.org” which is also known as CRAN.

  3. Destination for the installed package, which, in this case, is the directory we just created “~/local/R_libs”.

Using the user-level R package

To use the package from R, we call the function library. It is important to note that library by default only loads packages from root directory, so we have to specify location of the package whenever we try to include a package installed in our home directory as shown below.

If running an R script, this line must be included in any script needing access to the user library.

Quit the R console

Install R package from Release Binaries (without CRAN)

If the R package is not available on CRAN or you want to install an old version of packages, you can download the compressed file to your home directory and install it. In this section, we use ggplot2 2.0.0 as example.

R will attempt to install to a default directory located inside the R install path. This path is not writable by users and may cause user-level package installs to fail. Installing packages to the user home directory allows users to maintain their own R packages

Create a new directory

This helps keep things organized in the user home directory

Download packages

Since we know the URL, the command wget can be used to download the .tar.gz file. You can also copy the compressed file to the directory, if you have the file on your local machine or somewhere else in server.

 

Load latest version of R available on server. To check the latest version on server

A list of available module will pop up as shown below. You are able to see all versions of R available R on server.

 

For this tutorial, we will use R/4.1.0 To load R and run R console on Agave, enter:

Install package

Run the command below to install the package to a specific directory. If we do not specify the path, installation will fail since by default the the package will be installed in root directory which you do not have access to.

Run the package

Open the R console and load the package with the following command: