Integrate Workbench with reticulate and Python

These instructions describe how to use the reticulate R package with Posit Workbench to interface with Python.

Once you configure Python and reticulate with Workbench, users are able to develop mixed R and Python content with Shiny apps, R Markdown reports, and Plumber APIs that call out to Python code using the reticulate package.

What is reticulate?

The reticulate package provides a comprehensive set of tools for interoperability between Python and R. The package includes facilities for:

  • Calling Python from R in a variety of ways including from R Markdown, sourcing Python scripts, importing Python modules, and using Python interactively within an R session.
  • Translation between R and Python objects. For example, between R and Pandas data frames, or between R matrices and NumPy arrays.
  • Flexible binding to different versions of Python including virtual environments and Conda environments.

Reticulate embeds a Python session within your R session, enabling seamless, high-performance interoperability. If you’re supporting R developers that use Python for some of their work or a data science team that uses both languages, reticulate can dramatically streamline their workflow. The RStudio User Guide’s Getting started - Python section and the R Interface to Python guide may be a good reference to share with those users.

Requirements

RStudio uses the reticulate R package to interface with Python, and so the Python integration requires:

  • Installation of a supported Python version (see https://devguide.python.org/versions/)
  • The reticulate R package (1.20, or newer; as available from CRAN)

Step 1. Install Python for all users

  • First, follow the Install Python instructions on the server with Workbench in a central location for all users (e.g., /opt/python/3.11.5/).
  • Once you have completed the steps to Install Python, continue to Step 2.

Step 2. Install reticulate for all users

  • Install the reticulate R package for all users in the global R library. For example, if R is installed in /opt/R/4.3.1/, then you you can use the following command:

    $ sudo /opt/R/4.3.1/bin/Rscript -e 'install.packages("reticulate")'

Step 3. Configure reticulate with Python for all users

  • Set the RETICULATE_PYTHON environment variable for all Workbench users by putting the following line in the R session-specific profile script used by Workbench.
    For example, if Python is installed in /opt/python/3.11.5/, then you you can use the following configuration:

    File: /etc/rstudio/rsession-profile
    export RETICULATE_PYTHON=/opt/python/3.11.5/bin/python

Next Steps

Please see the RStudio User Guide to explore how Python can be used together with R and RStudio.