How to run multiple versions of R in RStudio Server Pro and in batch mode



Exporting certain environment variables in your .bash_profile may lead to unexpected results when selecting multiple versions of R. This happens particularly when prepending a specific version of R to the LD_LIBRARY_PATH environment variable. There are two workarounds. One solution is to write a new script that corrects for specific environment variables defined in .bash_profile and then tell RStudio Server Pro to execute this script every time a new session is run. The other solution is to append environment variables in .bash_profile so that they don’t conflict with the RStudio Server Pro environment. Both solutions will allow you to take advantage of multiple versions of R in RStudio Server Pro and in batch mode.


Some users need to execute specific versions of R when running R in batch mode. For example, a user may set Cron to run an R script every day using the RScript command. If there are multiple versions of R installed on the server, it’s important that the batch jobs run under a specific version of R. One way to do that is to define environment variables in the user’s .bash_profile that specify which version of R to run in batch mode.


RStudio Server Pro allows you to select from multiple versions of R from within the IDE. If you set certain environment variables in your .bash_profile you may get undersirable results in RStudio Server Pro. It is possible to select one version of R in RStudio Server Pro but load a different version based on your .bash_profile. The LD_LIBRARY_PATH environment variable in particular may cause the incorrect version of R to start up inside RStudio Server Pro.


If you choose to configure your .bash_profile so that a particular version of R starts up in batch mode, then all the environment variables will be passed into RStudio Server Pro prior to starting a new session. If you prepend your environment variables then your batch environment will take priority over the RStudio Server Pro environment.


For example, a user may select R-3.2.4 from the drop down menu in RStudio Server Pro, but the LD_LIBRARY_PATH in the .bash_profile might specifiy R-3.2.1. If the .bash_profile prepends the batch version such that LD_LIBRARY_PATH=/batch/R/path/R-3.2.1:$LD_LIBRARY_PATH then RStudio Server Pro will attempt to load R-3.2.1 in the session even though R-3.2.4 was selected.

Solution 1: Configure RStudio Server Pro

One solution is to configure RStudio Server Pro so that it overrides a specific set of environment variables. This can be done by customizing session launces as described in the admin guide. This approach basically requires the administrator to create a new file that will be executed prior to any new session being started. It involves the following steps.

Step 1. Identify the environment variables to be overridden

Let’s say this is your user’s .batch_profile. Notice that it is designed to launch R-3.2.1 in batch mode and exports three environment varialbes LD_LIBRARY_PATH, PATH, and MANPATH.


Step 2. Write a new script to overwrite the environment variables

Create a new script called /etc/rstudio/ that will prepend R_HOME to the environment variables in step 1 whenever RStudio Server Pro executes a new session. Make sure to execute the R session at the end by including exec $@.

exec $@

Step 3. Make the script an executible.

chmod 755 /etc/rstudio/

Step 4. Tell RStudio Server Pro to execute the new script

Next, configure RStudio Server Pro so that it will run the new script. This can be done by pointing the rsession-exec-command option in the /etc/rstudio/rserver.conf to the new script.


Step 5. Restart RStudio Server Pro

rstudio-server restart

Solution 2: Configure Bash Profile

An alternative workaround to experiencing conflicts in multiple versions of R between RStudio Server Pro and batch mode is to reverse the order of setting environment variables in .bash_profile. When environment variables are appended instead of prepended, they do not override the environment set by RStudio Server Pro.

# Prepended - May conflict with RStudio Server Pro R sessions

# Appended - Will not conflict RStudio Server Pro R sessions