Connecting R and Python

There are a few ways to do this, but the only one that worked for me was to use Rserve and rconnect. In R, do this:

 
install.packages("Rserve")
library(Rserve)
Rserve(debug = FALSE, port=6311, args=NULL)

Then you can connect in Python very easily. Here is a test in Python:

 
rcmd = pyRserve.rconnect(host='localhost', port=6311)
print(rcmd('rnorm(100)'))

Tags: , ,

2 comments

  1. You can look at the source code for Red-R (www.red-r.org) to get some examples of how to do this using RPy. Look in the RSession.py module in the canvas directory of where you install the software.

    Feel free to use that in your own programs if you want.

  2. Awesome. I have only heard about rpy2, as Kyle mention, before but Rserve seems interesting also. Will have look into Rserve in the future. Thank you for the heads up on that.

    Fredrik