10

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.

merv
  • 67,214
  • 13
  • 180
  • 245
Gregor Sturm
  • 2,792
  • 1
  • 25
  • 34

1 Answers1

18

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
merv
  • 67,214
  • 13
  • 180
  • 245
Gregor Sturm
  • 2,792
  • 1
  • 25
  • 34
  • Great answer! Although I'd set the `www-port` to `8787` instead, the default port for rstudio-server. Setting [rsession-which-r ](https://support.rstudio.com/hc/en-us/articles/200552316-Configuring-the-Server) to the R executable within a Conda environment kept giving me initialization errors. Using your answer I can now run rstudio-server within a Conda environment that will also correctly use the Python environment to use with the `reticulate` package. – Lodewic Van Twillert Oct 20 '18 at 09:15
  • Thanks, using the --secure-cookie-key-file option fixed an issue with running rocker/tidyverse via singularity on a multi-user cluster where rserver would exit with: `ERROR system error 2 (No such file or directory) [path=/tmp/rstudio-server/secure-cookie-key]; OCCURRED AT: rstudio::core:: ` – Tim May 12 '20 at 12:34