How to Set Up Conda-Installed R For Use With Rstudio

How to set up conda-installed R for use with RStudio?

So long as which R shows up a working R interpreter (which it should do if you have installed the r package from conda and activated your environment) then launching rstudio from that same environment should pick it up just fine.

For a test, on ArchLinux, I built and installed: https://aur.archlinux.org/packages/rstudio-desktop-git/

.. then force removed the R interpreter (pacman -Rdd r), then installed r from conda (conda install -c r r) and it worked fine. I then closed my terminal and opened a new one (so that the correct conda environment was not activated and successfully launched RStudio with the following command: RSTUDIO_WHICH_R=/home/ray/r_3_3_1-x64-3.5/bin/R rstudio

I think the crux is to launch RStudio from the right environment? Your ~/.bash_profile and ~/.bashrc are only sourced when you run bash. For environment variables to be set so that the your desktop environment knows about them, on Linux, you should put them in ~/.profile or else in /etc/pam.d (you may need to logout or shutdown after making those changes) and on OS X, you should check out https://apple.stackexchange.com/q/57385

Rstudio in anaconda environment: how should I install R packages?

Anaconda is a package manager/self-contained workspace and should be used to install packages if using R through a conda environment.

To install desolve, you'll want to run the below from your conda environment. You can search for the conda availability of an R package here: https://anaconda.org/conda-forge

conda install -c conda-forge r-desolve

In my experience, conda-forge often has more updated versions of R packages and one should look there first for the install.

Why does using conda to install rstudio, r version 4, using conda on my linux system return an error about incompatable specifications?

Looks like an issue with dependencies, as rstudio does not seem to be solvable with r-base=4.0.* specified.

This works:
conda create -c conda-forge -n r4rs r-base rstudio
This does not:
conda create -c conda-forge -n r4rs r-base=4.0.* rstudio

Probable reason might be that the rstudio package in the conda channel is not very actively maintained and hasn't seen an update in over a year, see here

Depending on what can give here, you could:

  1. Accept the option of conda create -c conda-forge -n r4rs r-base rstudio which will give you r-base<4.0

  2. Download and install your own version of rstudio from the official website and create an environment with just r-base==4.0.*, activate that env and then start your downloaded rstudio, which should detect the r you have installed into the conda env. See also this post

How can I use Conda environments with RStudio Server?

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

Working with an R package under a conda environment in macOS

Works for me, with some adjustments that I regard as better practice:

  • don't use RStudio from Conda - it is an abandoned project; see alternatives
  • only use conda-forge channel - mixing channels can have dynamic library issues
  • use a YAML for more reliable specification of requirements
  • explicitly declare R version (r-base)
  • include everything in the declared Imports (except what is included as dependencies of other packages)
  • conda-forge::r-cli>=3 builds are broken, so I pin that to newest working version
  • use mamba because conda is slow

Here is a YAML for creating the environment:

causalgps-env.yaml

name: causalgps
channels:
- conda-forge
dependencies:
- r-base=4.1
- r-tidyverse
- r-devtools
- r-xgboost
- r-superlearner
- r-earth
- r-ranger
- r-gam
- r-kernsmooth
- r-gnm
- r-polycor
- r-wcorr
- r-rlang
- r-glue
- r-logger
- r-cli>=2,<3

And the steps are:

  1. Create env.

    ## install Mamba if you don't have it
    ## conda install -n base conda-forge::mamba
    mamba env create -n causalgps -f causalgps-env.yaml
  2. Run R session in env.

    conda activate causalgps
    R
  3. Install package.

    library(devtools)
    install_github('fasrc/CausalGPS')
  4. Test loading.

    library(CausalGPS) ## works


Related Topics



Leave a reply



Submit