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:
- Install Rstudio: https://www.rstudio.com/products/rstudio/download/#download
- Install (Mini-)conda: https://conda.io/miniconda.html
- Add the
conda-forgechannel that contains ar-baseand lots ofRpackages:conda config --add channels conda-forge - Create a conda environment with a basic
Rinstallation:conda create -n R r-base r-tidyverse - Find the path to the
Rinstallation. The following should give you something like:/path/to/miniconda3/envs/R/bin/R:
conda activate R
whereis R
- Find the
rstudio.desktopfile:locate rstudio.desktop. This should give something like:/usr/share/applications/rstudio.desktop - Edit this file with
sudo, addingenv RSTUDIO_WHICH_R=/path/to/miniconda3/envs/R/bin/Rafter=sign of theExec=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.
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:
For example for me, the location is
/usr/share/applications/rstudio.desktopand its original contents are:Here, edit
Exec=/usr/lib/rstudio/rstudio %Fto instead read (where/path/to/...needs to be what you find with point5.above):Does this help?
Also, when launching RStudio from the command line, the
.desktopfile isn't used. So you have to manually provide the correct setting, e.g.: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/