Virtual Environment in R

Virtual environment in R?

It looks like there is another option from RStudio devs, renv. It's available on CRAN and supersedes Packrat.

In short, you use renv::init() to initialize your project library, and use renv::snapshot() / renv::restore() to save and load the state of your library.

I prefer this option to conda r-enviroments because here everything is stored in the file renv.lock, which can be committed to a Git repo and distributed to the team.

Can renv be used to create a virtual environment with a specific version of R?

You are correct that renv only manages the installed R packages, and not the R interpreter itself.

Depending on how you're using RStudio, you can still "fake" this by setting the RSTUDIO_WHICH_R environment variable. For example:

export RSTUDIO_WHICH_R=/path/to/R
rstudio

would tell RStudio to "bind" to the version of R specified by the RSTUDIO_WHICH_R environment variable.

For what it's worth, the ability to bind projects to a specific version of R is a feature of the professional editions of RStudio; however, it's not available in the open-source version. See here for more details.

How can I switch virtualenv from reticulate in R?

In short: By restarting the R session! You can't switch virtualenv in reticulate once chosen!

I tried it (but chose "venv2" first).


> use_python("venv1", T)
Error in use_python("venv1", T) :
Specified version of python 'venv1' does not exist.
> use_python("~/.virtualenvs/venv1", T)
ERROR: The requested version of Python ('~/.virtualenvs/venv1') cannot
be used, as another version of Python
('/home/josephus/.virtualenvs/venv2/bin/python') has already been
initialized. Please restart the R session if you need to attach
reticulate to a different version of Python.
Error in use_python("~/.virtualenvs/venv1", T) :
failed to initialize requested version of Python

So reticulate messages, that one has to start a new session to choose new virtual environment.
This must apply to use_virtualenv(<xxx>, T) too, though it is not as verbose as use_python(<xxx>, T).

Using system-wide python via reticulate

The following fixes the problem:

Sys.setenv(RETICULATE_PYTHON = "/home/x/.virtualenvs/r.reticulate/bin/python")
library(reticulate)

Set up conda environment for R package not on CRAN, installs to wrong location

First, the devtools isn't showing up because R packages in Conda repositories are conventionally prefixed with "r-", so installing conda install r-devtools should do the trick. However, I don't think Conda is the best strategy here.

Below R version 3.6, the Conda package coverage for R packages is rather poor. Also, installing non-Conda packages that require compilation into a Conda R environment is a pain and generally doesn't work out-of-the-box in my experience. Plus, not only does the TreeLS require compilation, but it has dependencies that are not Conda packages which require compilation. I would avoid this.

Option 1 is feasible. R allows multiple installations, and with manipulating environment variables (I think RSTUDIO_WHICH_R, R_LIBS are the pertinent ones) one can switch between them.

However, were this my situation, I'd spin up a docker container, probably rocker/rstudio:3.5 and use that for this project. Since the underlying image is Linux, it'll take awhile to compile, but you can version it at that point and then always have that available to spin up. This avoids having to muck around with any system settings and should be mostly straight-forward installing.



Related Topics



Leave a reply



Submit