One of the primary focuses of RStudio v1.2 is improved support for other languages frequently used with R. Last week on the blog we talked about new features for working with SQL and D3. Today we’re taking a look at enhancements we’ve made around the reticulate package (an R interface to Python).
The reticulate package makes it possible to embed a Python session within an R process, allowing you to import Python modules and call their functions directly from R. If you are an R developer that uses Python for some of your work or a member of data science team that uses both languages, reticulate can dramatically streamline your workflow. New features in RStudio v1.2 related to reticulate include:
Support for executing reticulated Python chunks within R Notebooks.
Display of matplotlib plots within both notebook and console execution modes.
Line-by-line execution of Python code using the reticulate
Sourcing Python scripts using the reticulate
Code completion and inline help for Python.
Note that for data science projects that are Python-only, we still recommend IDEs optimized for that, such as JupyterLab, PyCharm, Visual Studio Code, Rodeo, and Spyder. However, if you are using reticulated Python within an R project then RStudio provides a set of tools that we think you will find very useful.
You can download the RStudio v1.2 preview release here: https://www.rstudio.com/rstudio/download/preview/.
All of the features described below require that you have previously installed the reticulate package, which you can do as follows:
R Notebooks have been enhanced to support executing Python chunks using the reticulate Python engine. For example, here we use pandas to do some data manipulation then plot the results with ggplot2:
Python objects all exist in a single persistent session so are usable across chunks just like R objects. R and Python objects are also shared across languages with conversions done automatically when required (e.g. from Pandas data frame to R data frame or NumPy 2D array to R matrix).
The article on Calling Python from R describes the various ways to access Python objects from R as well as functions available for more advanced interactions and conversion behavior.
R Notebooks can also display matplotlib plots inline when they are printed from Python chunks:
See the article on the reticulate R Markdown Python Engine for full details on using Python chunks within R Markdown documents, including how to call Python code from R chunks and vice-versa.
You can execute code from Python scripts line-by-line using the Run button (or Ctrl+Enter) in the same way as you execute R code line-by-line. RStudio will automatically switch into reticulate’s
repl_python() mode whenever you execute lines from a Python script:
exit from the Python REPL to exit back into R (RStudio will also automatically switch back to R mode whenever you execute code from an R script).
Any Python objects created within the REPL are immediately available to the R session via the
reticulate::py object (e.g. in the example above you could access the pandas object via
In addition, RStudio now provides code completion and inline help for Python scripts:
Click the editor’s Source Script button (or the Ctrl+Shift+Enter shortcut) within a Python source file to execute a script using reticulate’s
Objects created within the script will be made available as top-level objects in the R global environment.
Since we released the package, we’re often asked what the source of the name “reticulate” is.
Here’s what Wikipedia says about the reticulated python:
The reticulated python is a species of python found in Southeast Asia. They are the world’s longest snakes and longest reptiles…The specific name, reticulatus, is Latin meaning “net-like”, or reticulated, and is a reference to the complex colour pattern.
And here’s the Merriam-Webster definition of reticulate:
1: resembling a net or network; especially : having veins, fibers, or lines crossing a reticulate leaf. 2: being or involving evolutionary change dependent on genetic recombination involving diverse interbreeding populations.
The package enables you to reticulate Python code into R, creating a new breed of project that weaves together the two languages.
UPDATE: Nov. 27, 2019
Learn more about how R and Python work together in RStudio.
In this series, we walk through lesser-known tips and tricks to help you work more effectively and efficiently in R Markdown. This third post focuses on features that save you time and trouble.
The RStudio IDE includes a visual markdown editor that displays changes in real-time and provides support for technical writing. This post walks through several features of visual editing mode.