How-To: Rstudio: Use Reticulate Package to Manage R-Python Integration

Question

How do I use the R reticulate package to manage R-Python integration?

 

Answer

1. Create and activate a new conda environment for use with R & Rstudio.

Note:  To create a conda environment for using Rstudio, see "How-To: Rstudio: Create Conda Environment for Using Rstudio"

 

2. Set conda-forge channel to highest priority in channel list.

 

3. Install any python packages needed to be used in the conda environment.

Example package: pandas

Note:  If using pandas as the example in Steps 8 & 9, pandas will need to be installed in Step 3.

 

4. Set path and launch Rstudio.

 

5. Create new R project without renv. The below instructions can be followed, but do not check the renv box.

Note:  To create a new project in Rstudio, see " How-To: RStudio: Create a New Project with Renv "

 

6. In the Rstudio console, install reticulate package using install.packages()

R prompt with "install.package(reticulate)"

 

7.  Load the reticulate package using library()

R prompt with "library(reticulate)"

 

8. Back in Anaconda Prompt, 

a. Enter "conda env list" to list conda environments.

b. Copy active environment to the clipboard.

Example active environment: rs_t3

Anaconda prompt with "conda env list" and command results.

 

9. In the Rstudio console, configure the conda environment using use_condaenv().

Example: use_condaenv(condaenv = "rs_t3")

R prompt with use_condaenv(condaenv="rs_t3")

 

10. Install python libraries using py_require().

Example: py_require("pandas") 

R prompt with py_require("pandas")

 

11. Import & use modules.

Example: pandas <- import("pandas")

R prompt with pandas <- import("pandas")