Running Rstudio from within a conda environment (and a feature suggestion)

I've seem a few posts about people struggling to use a conda environment with Rstudio (e.g. this one from @polklin).

At least on a Mac, you can get Rstudio to use your conda environment by going to your terminal and navigating to your project directory. From there, you can type "open project.Rproj" or "open my_file.R" and Rstudio will launch using your environmnent. To double check, you can run .libPaths() at the R prompt and you should see the path to your environment that looks something like this...

> .libPaths()
[1] "/Users/user_name/opt/miniconda3/envs/my_environment/lib/R/library"

You could also do this...

> system("conda env list")
# conda environments:
#
base                     /Users/user_name/opt/miniconda3
my_environment        *  /Users/user_name/opt/miniconda3/envs/my_environment
env-01                   /Users/user_name/opt/miniconda3/envs/env-01
testing                  /Users/user_name/opt/miniconda3/envs/testing
viz                      /Users/user_name/opt/miniconda3/envs/viz

as opposed to this when not using a conda environment...

> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"

It's frustrating to see so many responses on conda-related posts say that conda is only for projects with a lot of python. I don't know a lick of python and find conda to be very helpful for creating reproducible environments that have all sorts of other tools from specific versions of bash, snakemake, command line genomics programs, etc. Also, recall that while renv is great, it only manages packages and not the actual version of R. With conda we can set both the R version and R packages so that anyone can get a copy of my environment file and recreate the software environment I am using for an analysis (nb: I find that using renv with conda is challenging). It is the rare project that only uses R and conda provides a lot of tools that make reproducible integration of different tools possible. The most popular R packages are available on conda-forge in the latest versions and posting R versions of packages is relatively straightforward. Needless to say, this can be a complementary resource to CRAN.

As a feature suggestion, it would be great if a Rproj file could include the conda environment that is needed for the project and then have Rstudio use that environment. That would make the use of conda environments even easier for people to use.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.