Access two versions (or more !) of R within RStudio

This method is suited for computer running under the Linux Ubuntu OS.

Goal

For various reasons, we want to easily switch between two (or more!) version of the R language with RStudio.

From my side, I have a specific setup of R packages installed within a Singularity environment. But, I fail to access the version of R within this environment, using RStudio. So, I installed everything locally and ask RStudio to open the specific version of R.

Method

The method consists in:

  1. locate the path to R versions of interest
  2. define alias to open RStudio using each of these versions
  3. define desktop shortcuts to open RStudio in one click

Locate or install the R versions of interest

First, locate the folders where are installed the R versions of interest. For me, I have:

Version Folder
3.6.3 /usr/local/src/R-3.6.3
4.2.2 /usr/local/src/R-4.2.2
local_src

Exemple to install R version 3.6.3:

  1. Download the archive associated with the version of interest there: https://cran.r-project.org/src/base/ (eg. R-3.6.3.tar.gz)
version="3.6.3"

wget -O R-${version}.tar.gz https://cran.r-project.org/src/base/R-${version:0:1}/R-${version}.tar.gz
  1. Decompress the archive in the final folder.
# Move the archive in the final folder
sudo mv R-${version}.tar.gz /usr/local/src/

# Decompress the archive
cd /usr/local/src/
sudo tar -zxvf R-${version}.tar.gz

# Remove the archive
sudo rm -f R-${version}.tar.gz

# Change directory to the uncompress archive
cd R-${version}
  1. Install R.
sudo ./configure 
sudo make
sudo make install
  1. Check the installed version.
R --version

The default R version used by RStudio is accessible by the which R command. It can be /usr/bin/R, which is a shortcut for the last version installed. If you installed first R 4.2.2 and then R 3.6.3, /usr/bin/R --version will show the 3.6.3 version.

Note: Within RStudio, another R version can be forced by setting the RSTUDIO_WHICH_R variable in the ~/.Renviron (or ~/.profile or ~/.bashrc) file. For instance:

RSTUDIO_WHICH_R=/usr/local/src/R-4.2.2/bin/R

Define aliases

In the ~/.bashrc file, create as many aliases as you have R versions of interest.

alias r363="export RSTUDIO_WHICH_R='/usr/local/src/R-3.6.3/bin/R' rstudio"
alias r422="export RSTUDIO_WHICH_R='/usr/local/src/R-4.2.2/bin/R' rstudio"

Note: The above commands work for Ubuntu 24.04. However, to Ubuntu 20.04, a ; should be added:

alias r363="export RSTUDIO_WHICH_R='/usr/local/src/R-3.6.3/bin/R' ; rstudio"
alias r422="export RSTUDIO_WHICH_R='/usr/local/src/R-4.2.2/bin/R' ; rstudio"

To consider the changes, source the ~/.bashrc file:

source ~/.bashrc

You should now be able to open RStudio with R 3.6.3 (or R 4.2.2) by running the r363 (or r422) alias command in the terminal.

Note: Using the ~/.profile file to define an alias was not working on my computer, running under Ubuntu 20.04.

Define shortcuts

To avoid opening the terminal and typing 4 characters to open RStudio, we can define shortcuts ! The shortcut will be represented as a logo, pinned to the dashboard. For this, many tutorials exist. I followed the one from Rich Hewlett.

Create a .desktop file, for instance called rstudio_with_r363.desktop, containing:

[Desktop Entry]
Type=Application
Terminal=false
Name=RStudio R3.6.3
Icon=/path/to/a/beautiful/rstudio_pink.png
Exec=bash -c 'exec bash -i <<< "r363" '
Categories=Application;

Details:

  • Terminal=false to not open the terminal in background
  • Name is the hover text when you pass the cursor above the icon
  • Icon corresponds to the path to the icon
  • Exec is a command behind the shortcut. I found it on Stackoverflow
  • Do not forget the ; in the end

You may have as many .desktop files as you have R versions of interest.

To add the shortcuts in the dashboard from Ubuntu, save the .desktop files in the ~/.local/share/applications folder.

desktop_app

Then, pin the shortcut to the dashboard:

  • find the shortcut using its Name, in the Ubuntu Menu
  • right click to ‘Pin to Dash’

No need to restart the computer: everything should be well recognized !



Resources: