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:
- locate the path to R versions of interest
- define alias to open RStudio using each of these versions
- 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 |
Exemple to install R version 3.6.3:
- Download the archive associated with the version of interest there:
https://cran.r-project.org/src/base/(eg.R-3.6.3.tar.gz)
- Decompress the archive in the final folder.
- Install R.
- Check the installed 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:
Define aliases
In the ~/.bashrc file, create as many aliases as you have R versions of interest.
Note: The above commands work for Ubuntu 24.04. However, to Ubuntu 20.04, a ; should be added:
To consider the changes, source the ~/.bashrc file:
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:
Details:
Terminal=falseto not open the terminal in backgroundNameis the hover text when you pass the cursor above the iconIconcorresponds to the path to the iconExecis 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.
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: