Setting up and deploying a project with reticulate

Overview

Reticulate is an R package that enables you to execute Python code in an R session.

This can be useful if:

  • you have an existing R codebase that needs to integrate some Python functionality
  • you work with a team of data scientists who mostly use Python and want to be able to collaborate

Alternatives to reticulate

If you need to share data between R and Python, consider using arrow or a database.

If you need access to the result of a Python function call, consider exposing it as an API that you call from R.

Setting up a reticulated project

Note

The reticulate package includes helper functions like reticulate::use_python() and reticulate::virtualenv_create() that can be used to set up your project. The instructions below demonstrate setup at the terminal as an alternative.

  1. Create a virtual environment in the folder containing your reticulated R project.
  2. Add a .Renviron file to your R project where RETICULATE_PYTHON is set to the path of the Python executable in the virtual environment directory.
    • R will read this file when a new session starts and add it to your list of environment variables.
  3. Add a line to your .gitignore file for your .Renviron.
  4. Restart your R session.

Terminal

WDAGUtilityAccount@mvp MINGW64 ~/Documents
$ mkdir my-reticulated-project

WDAGUtilityAccount@mvp MINGW64 ~/Documents
$ cd my-reticulated-project

WDAGUtilityAccount@mvp MINGW64 ~/Documents/my-reticulated-project
$ python -m venv .venv

WDAGUtilityAccount@mvp MINGW64 ~/Documents/my-reticulated-project
$ echo "RETICULATE_PYTHON=.venv/bin/python" >> .Renviron

WDAGUtilityAccount@mvp MINGW64 ~/Documents/my-reticulated-project
$ echo ".Renviron" >> .gitignore

To confirm that this is set correctly, retrieve the value of RETICULATE_PYTHON from the R console.

R

> Sys.getenv("RETICULATE_PYTHON")
[1] ".venv/bin/python"

Deploying a reticulated project

Connect rebuilds environments to execute your content based on the manifest. To successfully deploy reticulated content to Connect, your manifest must indicate that both R and Python are required runtimes:

manifest.json

{
  "version": 1,
  "locale": "en_US",
  "platform": "4.1.0",
  "metadata": {
    "appmode": "shiny",
    "primary_rmd": null,
    "primary_html": null,
    "content_category": null,
    "has_parameters": false
  },
  "python": {
    "version": "3.8.6",
    "package_manager": {
      "name": "pip",
      "version": "21.2.4",
      "package_file": "requirements.txt"
    }
  },

In particular, the help for rsconnect::writeManifest() states:

 python: Full path to a python binary for use by ‘reticulate’. The
          specified python binary will be invoked to determine its
          version and to list the python packages installed in the
          environment. If python = NULL, and RETICULATE_PYTHON is set
          in the environment, its value will be used.

Setting RETICULATE_PYTHON to the Python executable in your virtual environment will:

  • ensure that a Python section appears in your manifest.json
  • ensure that _only the packages installed in your virtual environment are written in to the requirements.txt file

If no requirements.txt file is found in the project directory, one will be generated for you, but in general you should generate it explicitly yourself by calling pip freeze.

Warning

When the manifest contains a Python section, Connect assumes responsibility for building a Python environment based on the packages enumerated in the requirements.txt file. You do not need to (and should not) include reticulate functions for installing Python or managing environments (e.g. reticulate::install_*(), reticulate::use_*(), reticulate::virtualenv_*(), reticulate::conda_*()) in your deployed R code.

Examples

Visualizing pandas dataframes with ggplot2

Image classification with PyTorch and Shiny

Sentiment analysis with Plumber and spaCy

Further reading

Reticulate package site

RViews article on reticulated Shiny

Back to top