Call R from Python

colin@mikrojenkins:~$ mkdir mikroR
colin@mikrojenkins:~$ cd mikroR
colin@mikrojenkins:~/mikroR$ virtualenv .
New python executable in ./bin/python
Installing setuptools, pip…done.
colin@mikrojenkins:~/mikroR$ . bin/activate
(mikroR)colin@mikrojenkins:~/mikroR$ pip install rpy2
….
error: Setup script exited with Error: Tried to guess R’s HOME but no R command in the PATH.

(mikroR)colin@mikrojenkins:~/mikroR$ sudo apt-get update
[sudo] password for colin:
(mikroR)colin@mikrojenkins:~/mikroR$ sudo apt-get install r-base -y
(mikroR)colin@mikrojenkins:~/mikroR$ pip install rpy2
Successfully installed rpy2

(mikroR)colin@mikrojenkins:~/mikroR$ cat app.py
#!bin/python
import rpy2.robjects as robjects

rplot = robjects.r[‘plot’]
rpng = robjects.r[‘png’]

vol = robjects.r[‘volcano’]
v = robjects.FloatVector([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
m = robjects.r[‘matrix’](v, nrow = 2)

rpng(“Routput/m.png”, width=600, height=589)
print(m)
rplot(m)

rpng(“Routput/vol.png”, width=600, height=589)
rplot(vol)

(mikroR)colin@mikrojenkins:~/mikroR$ ./app.py
[,1] [,2] [,3]
[1,] 1.1 3.3 5.5
[2,] 2.2 4.4 6.6

colin@mikrojenkins:~/mikroR$ ls Routput/
m.png vol.png

Leave a Reply

Your email address will not be published. Required fields are marked *