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 on command line

 

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 with active environment rs t3 being selected for copy.

 

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

Example: use_condaenv(condaenv = "rs_t3")

R prompt with use_condaenv condaenv equals sign rs underscore t3

 

10. Install Python libraries using py_require().

Example: py_require("pandas") 

R prompt with py_require opening parentheses quote mark pandas quote mark closing parentheses

 

11. Import & use modules.

Example: pandas <- import("pandas")

R prompt with pandas less than symbol and hyphen forming a left pointing arrow import left parentheses double quotation mark pandas quotation mark right parentheses

Print Article

Related Articles (2)

Quick start guide to using R, RStudio and Python together on University-managed computers including installation without administrator credentials, utilization of unique Conda environments for each project, adding additional channels for packages, adding R and Python packages to the environment, launching RStudio, utilizing the reticulate R package and backing up configurations for portability and reproducibility.
Index of Advanced Topics for Conda, Python, and R