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.

1 Answer 1

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
2
  • Great answer! Although I'd set the www-port to 8787 instead, the default port for rstudio-server. Setting [rsession-which-r ](support.rstudio.com/hc/en-us/articles/…) 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. Commented Oct 20, 2018 at 9: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:: <snip>
    – Tim
    Commented May 12, 2020 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.