Skip to content

Instantly share code, notes, and snippets.

@magic-lantern
Last active April 30, 2024 13:18
Show Gist options
  • Save magic-lantern/c66431b3f613258b94b6fdd79ac1628a to your computer and use it in GitHub Desktop.
Save magic-lantern/c66431b3f613258b94b6fdd79ac1628a to your computer and use it in GitHub Desktop.
How to use R, RStudio, and conda

How to use R, RStudio, and conda

I created a post to try to combine questions/issues people had as well as solutions I found.

See https://forum.posit.co/t/rstudio-and-conda/186130

RStudio Desktop

Key here is to make sure both R and LD_LIBRARY_PATH are coming from the desired conda environment.

conda create -n myrenv r-base=4.3
conda activate myrenv
export ORIG_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=${CONDA_PREFIX}/lib/R/lib:${CONDA_PREFIX}/lib:$LD_LIBRARY_PATH

Then run rstudio from the same command line. Can also add the above to your `.bashrc'

When done, do this to revert your environment back to normal:

conda deactivate
export LD_LIBRARY_PATH:$ORIG_LD_LIBRARY_PATH

RStudio Server

Variables needed for RStudio Server - community edition. RStudio Server 2023.12.1 Build 402

rstudio/rstudio#14001 (comment)

Did first have to adjust /etc/rstudio/rserver.conf to allow my user to update.

echo "rsession-which-r=${CONDA_PREFIX}/bin/R" >> /etc/rstudio/rserver.conf
echo "rsession-ld-library-path=${CONDA_PREFIX}/lib" >> /etc/rstudio/rserver.conf

Here's what my configuration file looks like:

cat /etc/rstudio/rserver.conf

# Server Configuration File
# prevent signout when using shiny apps
auth-timeout-minutes=0
auth-stay-signed-in-days=99
# use custom conda environment for R
rsession-which-r=/opt/miniconda3/envs/aim2_r_python_v2/bin/R
rsession-ld-library-path=/opt/miniconda3/envs/aim2_r_python_v2/lib/R/lib:/opt/miniconda3/envs/aim2_r_python_v2/lib

Multiple RStudio Servers

Using the above, along with a customized startup script, it should be possible to have a separate RStudio Server per conda environment. Each RStudio Server instance would need to have a custom port and custom configuration path.

I haven't tested this yet, so there may be some gotchas...

Create a separate /etc/rstudio/rserver.conf file for each server desired with the variables mentioned above, then create a separate service configuration file for each server.

cat /lib/systemd/system/rstudio-server.service

[Unit]
Description=RStudio Server for myrenv
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
# each server needs this customized
PIDFile=/run/rstudio-server-myrenv.pid
# can use line similar to the below to specify location of configuration files
# each server needs this customized
Environment="RSTUDIO_CONFIG_DIR=/etc/config/rstudio" "XDG_CONFIG_DIRS=/etc/config"
ExecStart=/usr/lib/rstudio-server/bin/rserver
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

This document references RStudio Server Pro, but the specifics about how to specify a custom configuration directory work for the free/community edition.

https://docs.posit.co/ide/server-pro/1.4.1021-2/server-management.html

Related documentation on setting up services:

https://www.baeldung.com/linux/systemd-multiple-parameters

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