RStudio and Conda

I have a solution to share related to previous posts.

Using RStudio within a conda environment asked by @polklin with the accepted solution of "As far as I know RStudio doesn't officially support running the R interpreter (or R core in Anaconda terms) trough a conda environment."

I've found a way to make RStudio Desktop IDE and RStudio Server work within a conda environment. RStudio requires both the binary R and shared libraries to be in the path of the user calling rstudio On Ubuntu 22.04 with current conda and Rstudio version 2023.12.1+402 this requires a small manual step. I believe this solution will at least work for other versions of Linux.

Here's an example of what is needed:

conda create -n myrenv r-base=4.3
conda activate myrenv

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

conda deactivate

Also related: RStudio start-up problem: unable to initialize the JIT @ugroempi with 1 reply, but no accepted solution.

I was getting a similar error:

2024-04-30T12:43:34.785181Z [rsession-user] ERROR R SUICIDE: unable to initialize the JIT; LOGGED FROM: void {anonymous}::rSuicide(const string&) src/cpp/session/SessionMain.cpp:1245

And found that the problem in my case was due to Rstudio getting R from my path, but using system libraries from the system R - so R and were not compatible with each other.

1 Like

On a related topic, the solution of setting the path to R and shared libraries also works for RStudio Server, community edition. I have tested this on Ubuntu 20.04 (Focal) with version 2023.12.1 Build 402

However, the variables that need to be set have a different name than those used in bash.

See RStudio can't find · Issue #14001 · rstudio/rstudio · GitHub for the solution.

Basically, follow these steps:

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

If you want to revert the change so you can use the system R again, just comment out or remove those 2 lines.

1 Like

@magiclantern Thanks for sharing your solution! Being able to use R from conda within RStudio is very useful, so I'm sure others will appreciate you taking the time to document this.

A few comments to add:

  • The GitHub project grst/rstudio-server-conda provides scripts to run RStudio Server in a conda environment hosted inside of a Docker/Singularity container. You might be able to learn some tips and tricks from this approach. For example, adding options like rsession-which-r to /etc/rstudio/rserver.conf and then having to comment them out is cumbersome. Instead you can pass these as command line flags directly to rserver
  • Years ago I used to be able to activate my conda environment and then directly invoke rstudio in the terminal to open RStudio Desktop that automatically used the R that was installed in the conda environment. This was using Ubuntu. Do you get errors when you try that approach?

  • Just a general comment. It's a shame that Discourse doesn't display a link to this thread at the bottom of the other thread (kind of like how GitHub Issues/PRs display links to other discussions that have mentioned them). Since that previous thread is locked, there is no easy way for someone reading that thread to find this nice solution

1 Like

@jdblischak - Thanks so much for the reply!

Will need to do more testing with the GitHub project you mentioned, but I can answer one of your questions right now about RStudio Desktop IDE: I've found that if the system R and the conda environment R are the same version (tested with 4.1.x) then you can run Rstudio Desktop inside a conda R environment with no special setup required as it seems that part of the system R and conda R is used when calling RStudio Desktop. New package installations are only done for the conda R.

If however, you have system R of 4.1 and a conda environment with R 4.3, Rstudio Desktop IDE will not work unless you set the environment variable LD_LIBRARY_PATH as I showed in RStudio and Conda There may exist combinations of R versions that are compatible with each other, but in my testing on Ubuntu, this was my experience.

edit: clarified some of my reply

1 Like

I agree with the previous post that it is frustrating that Discourse doesn't link to related discussion threads. With the forced autoclosure and no possibility of re-opening, it is possible that people will find those old posts with no real solution and think that what they've read is the final answer.

I wish I would have come across Gregor Sturm's project 6 months ago. Would have saved me a lot of work! Based on the name, it appears @Gregor_Sturm is the same person here on this forum. Perhaps Gregor can help others as well.

1 Like