Python interface for R Programming Language [duplicate]
Asked Answered
D

3

54

I am quite new to R, and pretty much used to python. I am not so comfortable writing R code. I am looking for python interface to R, which lets me use R packages in pythonic way.

I have done google research and found few packages which can do that:

But not sure which one is better ? Which has more contributers and more actively used ?

Please note my main requirement is pythonic way for accessing R packages.

Dendy answered 30/7, 2012 at 7:17 Comment(1)
You may want to add reticulate to the list cran.r-project.org/web/packages/reticulate/vignettes/…Justiceship
P
66

As pointed out by @lgautier, there is already another answer on this subject. I leave my answer here as it adds the experience of approaching R as a novice, knowing Python first.


I use both Python and R and sympathise with your need as a newcomer to R.

Since any answer you get will be subjective, I summarise a few points from my experience:

  • I use rpy2 as my interface and find it is 'Pythonic', stable, predictable, and effective enough for my needs. I have not used the other packages so this is not a comment on them, rather on the merits of rpy2 itself.
  • BUT do not expect that there will be an easy way of using R in Python without learning both. I find that adding an interface between the two languages allows ease of coding when you know both, but a nightmare of debugging for someone who is deficient in one of the languages.

My advice:

  1. For most applications, Python has packages that allow you to do most of the things that you want to do in R, from data wrangling to plotting. Check out SciPy, NumPy, pandas, BioPython, matplotlib and other scientific packages, or even the full Anaconda or Enthought python distributions. This allows you to stay within the Python environment and provides you most of the power that you need.
  2. At the same time, you will want R's vast range of specialised packages, so spend some time learning it in an interactive environment. I found it almost impossible to master even basic R on the command line, but RStudio and the tutorials at Quick-R and Learn-R got me going very fast.

Once you know both, then you will do magic with rpy2 without the horrors of cross-language debugging.


New Resources

Update on 29 Jan 2015

This answer has proved popular and so I thought it would be useful to point out two more recent resources:

The triplet R, Rserve, and pyRserve allows the building up of a network bridge from Python to R: Now R-functions can be called from Python as if they were implemented in Python, and even complete R scripts can be executed through this connection.

  • It is now possible to combine R and Python using rmagic in IPython/Jupyter greatly easing the work of producing reproducible research and notebooks that combine both languages.
Promptbook answered 30/7, 2012 at 7:36 Comment(6)
I found debugging R code from within Python quite hard. For example, browser statements where not picked up. What is you experience?Chartres
@PaulHiemstra: same here. I could never be sure whether something failed to work (a) because I had a bug in Python or R or (b) because rpy2 was not working or (c) a combination. Nowadays I code the bit I need in R directly and then execute the script from within Python if I need it.Promptbook
browser is currently not working from within rpy2 ( bitbucket.org/lgautier/rpy2/issue/66/…). An error happening during the evaluation of R code will raise an RRuntimeError exception, which should tell where the point of failure was in R or Python.Gramophone
Have you used any other package than Rpy2 ! Are they any good ?Dendy
No, sorry, @darshan. I leave it to others to list experiences on those packages. The other answers that have been referenced, though, have some convincing pros and cons that will get you started. Simply looking at their docs will help assess how well they fit your style of coding.Promptbook
I especially appreciate you saving me hours of debugging... " but a nightmare of debugging for someone who is deficient in one of the languages"Karwan
G
5

A question about comparing rpy2, pyrserve, and pyper with each other was answered on the site earlier.

Regarding the number of contributors, I'd say that all 3 have a relatively small number. A site like Ohloh can give a more detailled answer.

How actively a package is used is tricky to determine. One indication might be the number of downloads, an other might be the number of posts on mailing lists or the number questions on a site like stackoverflow, the number of other packages using it or citing it, the number of CVs or job openings mentioning the package. As much as I believe that I could give a fair evaluation, I might also be seen as having a conflict of interest. ;-)

All three have their pros and cons. I'd say that you base you choice on that.

Gramophone answered 30/7, 2012 at 8:20 Comment(2)
Totally fair, Laurent, and thanks for the great work on rpy2 :)Promptbook
In rpy2 is the only project that's listed in Ohloh. But that's fine. From forums look's like rpy2 looks more talked in stack-overflow. If I get struck in the process, I have you guys to ask. Thanks for the response :)Dendy
C
4

My personal experience has been with Rpy, not Rpy2. I used it for a while, but dropped it in favor of using system commands. A typical case for me was running a FORTRAN model using Python scripts, and post-processing with R. In my experience the easiest solution was to create a command line tool using R, which is quite straightforward (at least under Linux). The command line tool could be executed in the root of the model run, and the script would produce a set of R objects and plots in an Routput directory. The advantage of disconnecting R and Python in this way was that I could easily debug the R code separate from the Python code.

I think Rpy really shines when a lot of back and forth communication between R and Python is needed. But if the functionality is nicely separable, and the overhead of disk i/o is not too bad, I would stick to system calls. See ?system for more information regarding system calls, and Rscript for running R scripts as a command line tool.

Regarding your wish to write R code in a Python way, this is not possible as all the solutions require you to write R code in R syntax. For Rpy this means R syntax, but a little different (no . for example). I agree with @gauden that there is no shortcut in using R through Rpy.

Chartres answered 30/7, 2012 at 9:15 Comment(1)
Code snippets for how to set up a function call in R to be run from Python in the way that you describe are provided here.Yaker

© 2022 - 2024 — McMap. All rights reserved.