Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Conda environments with RStudio Server?

For my data science projects, I use conda to keep track of all installed packages.

> conda create -n my_project R=3.4.1 r-tidyverse
> conda activate my_project
> which R 
/storage/apps/anaconda3/envs/my_project/bin/R
> R --version
R version 3.4.1 (2017-06-30) -- "Single Candle"

How can I use the Conda environment's R within rstudio-server? I am aware of the rsession-which-r configuration option, but like that I cannot easily switch between environments.

like image 798
Gregor Sturm Avatar asked Sep 18 '18 07:09

Gregor Sturm


Video Answer


1 Answers

I created a GitHub repository containing two scripts that allow you to start Rstudio server in non-daemonized mode from within a Conda environment: rstudio-server-conda.

How it works:

You can start rstudio-server in the non-daemonized mode (similar to jupyter notebook) from within a Conda environment.

> conda activate my_project
> /usr/lib/rstudio-server/bin/rserver \
   --server-daemonize=0 \
   --www-port 8787 \
   --rsession-which-r=$(which R) \
   --rsession-ld-library-path=$CONDA_PREFIX/lib

To avoid additional problems with library paths, also rsession needs to run within the Conda environment. This is achieved by wrapping rsession into the rsession.sh script. The path to the wrapped rsession executable can be passed to rserver as command line argument.

rserver # ...
    --rsession-path=rsession.sh

Finally, when using multiple users a unique secret-cookie-key has to be generated for each user. The path to the secret cookie key can be passed to rserver as a command line parameter.

uuid > /tmp/rstudio-server/${USER}_secure-cookie-key
rserver # ...
   --secure-cookie-key-file /tmp/rstudio-server/${USER}_secure-cookie-key
like image 159
Gregor Sturm Avatar answered Oct 23 '22 09:10

Gregor Sturm