We are pleased to announce the reticulate package, 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 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 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!
You can install the reticulate pacakge from CRAN as follows:
install.packages(&quot;reticulate&quot;)
Read on to learn more about the features of reticulate, or see the reticulate website for detailed documentation on using the package.
Python in R Markdown
The reticulate package includes a Python engine for R Markdown with the following features:
Run Python chunks in a single Python session embedded within your R session (shared variables/state between Python chunks)
Printing of Python output, including graphical output from matplotlib.
Access to objects created within Python chunks from R using the py object (e.g. py$x would access an x variable created within Python from R).
Access to objects created within R chunks from Python using the r object (e.g. r.x would access to x variable created within R from Python)
Built in conversion for many Python object types is provided, including NumPy arrays and Pandas data frames. From example, you can use Pandas to read and manipulate data then easily plot the Pandas data frame using ggplot2:
Note that the reticulate Python engine is enabled by default within R Markdown whenever reticulate is installed.
See the R Markdown Python Engine documentation for additional details.
Importing Python modules
You can use the import() function to import any Python module and call it from R. For example, this code imports the Python os module and calls the listdir() function:
library(reticulate)
os