R Interface to Python
The reticulate package provides an R interface to Python modules, classes, and functions. For example, this code imports the Python
os module and calls some functions within it:
library(reticulate) os <- import("os") os$chdir("tests") os$getcwd()
Functions and other data within Python modules and classes can be accessed via the
$ operator (analogous to the way you would interact with an R list, environment, or reference class).
When calling into Python, R data types are automatically converted to their equivalent Python types. When values are returned from Python to R they are converted back to R types. Types are converted as follows:
|List of multiple types||Tuple||
|NULL, TRUE, FALSE||None, True, False||
If a Python object of a custom class is returned then an R reference to that object is returned. You can call methods and access properties of the object just as if it was an instance of an R reference class.
The reticulate package is compatible with all versions of Python >= 2.7. Integration with NumPy is optional and requires NumPy >= 1.6.
First, install reticulate from CRAN as follows:
Then, learn the basics by reading the Introduction to reticulate.
The following articles cover the various aspects of using reticulate: