Integrate Workbench with reticulate and Python

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

Advantages of using reticulate

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 who use Python for some of their work or a data science team that uses both languages, reticulate can dramatically streamline their workflow. The Getting started - Python section in the RStudio User Guide and the R Interface to Python documentation 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. (Optional) Configure reticulate with a default Python environment for all users

Note

This step is optional. If it is skipped, the first time each individual user uses reticulate, they will be prompted to create a default virtual environment.

Normally, users will be responsible for the selection and creation of the Python environments they wish to use with reticulate. However, if you’d like to initialize a default Python environment for your users, you can do so with the following steps.

# Get the list of users from rstudio-server and iterate over them
sudo rstudio-server list-users | while read -r user; do
    echo "Creating virtual environment for user: $user"
    # Run the Rscript command as the specified user
    sudo -u "$user" /opt/R/4.3.1/bin/Rscript -e \
      'reticulate::virtualenv_create("r-reticulate", version = "3.11.5")'
done

Next Steps

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

Back to top