Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python interface for R Programming Language [duplicate]

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:

  • Rpy2
  • PypeR
  • pyRserve

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.

like image 960
darshan Avatar asked Jul 30 '12 07:07

darshan


People also ask

Can R and Python be used together?

Using R and Python together at the same time is incredibly easy if you already have your R scripts prepared. Calling them from Python boils down to a single line of code.

Is R language same as Python?

The main difference is that Python is a general-purpose programming language, while R has its roots in statistical analysis. Increasingly, the question isn't which to choose, but how to make the best use of both programming languages for your specific use cases.

Why are R and Python so similar?

R and Python are both open-source programming languages with a large community. New libraries or tools are added continuously to their respective catalog. R is mainly used for statistical analysis while Python provides a more general approach to data science.

Can R be replaced by Python?

The answer is yes—there are tools (like the feather package) that enable us to exchange data between R and Python and integrate code into a single project.


2 Answers

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:

  • Ralph Heinkel gave a great talk on this subject at EuroPython 2014. The video on Combining the powerful worlds of Python and R is available on the EuroPython YouTube channel. Quoting him:

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.
like image 176
daedalus Avatar answered Sep 21 '22 02:09

daedalus


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.

like image 33
lgautier Avatar answered Sep 21 '22 02:09

lgautier