Skip to content

Instantly share code, notes, and snippets.

@dlaehnemann
Last active August 25, 2023 09:39
Show Gist options
  • Save dlaehnemann/8126c903d560307cb246fa26b52d10de to your computer and use it in GitHub Desktop.
Save dlaehnemann/8126c903d560307cb246fa26b52d10de to your computer and use it in GitHub Desktop.
Using Rstudio desktop with a custom R installation under Ubuntu 18.04 (in this case, conda)

I try to use conda for as many software installations in data analysis as possible, as this will ususally guarantee that I can use up to date versions of that software.

This now includes R, and as I mostly use R through Rstudio Desktop, I checked how I can use a custom R installation with Rstudio Desktop. As described in the Rstudio docs, you just have to set the environment variable RSTUDIO_WHICH_R to the R binary of your wanted installation. For this environment variable to be set when launching Rstudio from Ubuntu's application launcher, you have to make the respective environment setting in the rstudio.desktop file.

As a reminder for me, and maybe as info for others, the general setup is:

  1. Install Rstudio: https://www.rstudio.com/products/rstudio/download/#download
  2. Install (Mini-)conda: https://conda.io/miniconda.html
  3. Add the conda-forge channel that contains a r-base and lots of R packages: conda config --add channels conda-forge
  4. Create a conda environment with a basic R installation: conda create -n R r-base r-tidyverse
  5. Find the path to the R installation. The following should give you something like: /path/to/miniconda3/envs/R/bin/R:
conda activate R
whereis R
  1. Find the rstudio.desktop file: locate rstudio.desktop. This should give something like: /usr/share/applications/rstudio.desktop
  2. Edit this file with sudo, adding env RSTUDIO_WHICH_R=/path/to/miniconda3/envs/R/bin/R after = sign of the Exec= key.

If you need further R packages, search for them with conda, usually using the prefix r-, so e.g.:

conda search r-extrafont

If you find the package, install it into your r environment:

conda install -n R r-extrafont

If the package does not have a conda-forge feedstock, yet, consider contributing by creating a conda-forge recipt from the conda R skeleton helper.

@dlaehnemann
Copy link
Author

You should not have to activate your conda environment, but the correct R should be used automatically when starting RStudio via the graphical interface of your GNOME desktop manager. It should suffice to do the edit in the file that you get via the command:

locate rstudio.desktop

For example for me, the location is /usr/share/applications/rstudio.desktop and its original contents are:

❯ cat /usr/share/applications/rstudio.desktop
[Desktop Entry]
Exec=/usr/lib/rstudio/rstudio %F
Icon=rstudio
Type=Application
Terminal=false
Name=RStudio
Categories=Development;IDE;
MimeType=text/x-r-source;text/x-r;text/x-R;text/x-r-doc;text/x-r-sweave;text/x-quarto-markdown;text/x-r-markdown;text/x-r-html;text/x-r-presentation;application/x-r-data;application/x-r-project;application/x-rdp-rsp;text/x-r-history;text/x-r-profile;text/x-tex;text/x-markdown;text/css;text/javascript;text/x-chdr;text/x-csrc;text/x-c++hdr;text/x-c++src;

Here, edit Exec=/usr/lib/rstudio/rstudio %F to instead read (where /path/to/... needs to be what you find with point 5. above):

Exec=env RSTUDIO_WHICH_R=/path/to/miniconda3/envs/R/bin/R /usr/lib/rstudio/rstudio %F

Does this help?

Also, when launching RStudio from the command line, the .desktop file isn't used. So you have to manually provide the correct setting, e.g.:

RSTUDIO_WHICH_R=/path/to/miniconda3/envs/R/bin/R rstudio

You could make this a shell alias for a particular conda environment. For a guide on how to create and use shell aliases, for example see here: https://www.howtogeek.com/439736/how-to-create-aliases-and-shell-functions-on-linux/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment