For a side project, I needed to run a piece of Python code from R. Given their popularity (e.g., they hold the spots #2 and #9 in the PYPL popularity of programming languages index) and their focus on being pragmatic, I thought this should be very simple.
I was wrong. There seems to be an easy solution to call R from Python (via rpy) but the other way around turns out to be much more difficult. The packages that promise to do so (such as rPython or rPythonWin) seem to be not really in widespread use, and are thus not maintained very well1.
I shouldn’t complain about open source (and send patches instead ;-), but there seems to be no well-maintained, easy-to-use, cross-platform R plugin that allows to call Python. Since I only have to make a few calls to a single Python function and runtime performance is not an issue, I gave up and simply used JSON and the command line for data transfer and invocation.
Here are some code samples that should get you going, if you also just want to cobble something together without being particularly proficient in either language.
The R snippet (requires an
install.packages("rjson") in the R console):
The Python snippet for
in the R console, the intermediate files
python_output.json look as expected (before being deleted):
This approach is far from perfect (for example: no debug mode that keeps the intermediate files, no support for multi-threading, assumes
python to be available on the
PATH, python command-line options like
-O are missing), but it is also simple enough to understand this again in a few months, it requires no extra setup, and it works cross-platform (I hope ;-).
This is a euphemism for me not being able to set them up, neither on Windows nor on Linux, and neither with a current R version (3.3.2) nor an old one (2.15.1) that they should support (according to their documentation). ↩