This page will be useful for those who enjoys working in RStudio, with majority of code written in R, but also utilizing python packages. Information here extend that presented on the page describing the use of renv on cluster: this page. It is recommended you read that page first.
Here you will see how to use renv to track both R packages and Python packages. In this workflow renv will automatically create conda environment inside R project directory to handle Python packages. This workflow assumes that you have R and Anaconda installed locally (if you work on cluster you can rely on R and anaconda modules).
Check that you have installed R and Anaconda. For R make sure you have a version you want selected in RStudio (Tools > Global Options > R version)
Create new RStudio project: RStudio -> new project -> new directory
install.packages(renv)renv::activate()renv::use_python(type = "conda")Make anaconda available in the project. The path you specify depends on which anaconda version in the system you are using.
In order to do that, add the following line into .Rprofile file. Close and open R project.
Now you can specify you are going to use python packages, and that those packages will be handled with anaconda. The following command will create conda environment withing renv directory
Install R packages as usual
Install python packages using. Remember to change cache directory if applicable (as described in renv page).
Make snapshot of current package versions
renv::snapshot()snapshot command will create two files in the project directory
renv.lock - This file will contain info about all the R packages used, R version info, info on conda env
environment.yml - this file will contain info about all the conda packages installed.
Commit those files to git together with your script files
Assuming you have renv.lock and environment.yml file in the project directory you would use `renv::restore()` command. This will install R packages and conda packages (including python itself). You need to make sure that R version you use is the same as the one in renv.lock
This should work nicely as long as you restore at the same OS, at which renv::snapshot() was created (Windows, Linux, macOS)
In case you or your collaborators are going to reproduce the same environment on a different OS (for example Widnows -> Linux or Linux -> Windows), you may need to change content of this file. In particular
Recreate environment.yml file using commands:
This will overwrite the one created by renv::snapshot(), which does include build information, which is specific to OS, and may create problems for environment setup on another OS
change name parameter within the file to null
change prefix parameter within file to appropriate value
For Linux and MacOS
./renv/python/condaenvs/renv-python
For Windows
.\renv\python\condaenvs\renv-python
Some conda packages are only available on Linux or only on Windows.
While executing restoration command renv::restore() you may see that some conda packages are not available. One of the most likely reasons is that those packages are specific to operating system. Examples: vs2015, ucrt, vc are Windows specific; libnsl, libgfortran5 are Linux specific. Thus, you may need to delete those lines from the file for renv::restore() to work properly
You can use Open OnDemand, open RStudio and create RStudio project the same way you would do locally: either by creating a project in a new directory, or by cloning from the GitHub repository.
These pages may be useful:
https://mpn.metworx.com/packages/renv/0.12.5/reference/use_python.html
https://rstudio.github.io/reticulate/articles/python_packages.html