Skip to content
Prev 207179 / 398503 Next

use R from python

Hi Barry,

thanks for your preciouse help!
Please apologize my newbie questions.

i'm able to go ahead tring to port the R code to run under python,
now i'm blocked on the "plot" function.

maybe i'm doing the same error but i haven't yet find a way to debug
my code line.

this what i tried :

# import library (rpy2 + spgrass6)

import rpy2.rinterface as rinterface
import rpy2.robjects as robjects
robjects.r.require('spgrass6', quietly = True)[0]


# def some functions :
rsort = robjects.r['sort']
rseq = robjects.r['seq']
rlength = robjects.r['length']
rdiv = robjects.r['/']
rmin = robjects.r['min']
rmax = robjects.r['max']
rdiff = robjects.r['-']
rget = robjects.r['get']
rslot = robjects.r['slot']
rpng = robjects.r['png']
rplot = robjects.r['plot']
df = robjects.r['data.frame']

# input data and output image
inputmap = 'elevation.dem'
output_image = '/Users/sasha/Desktop/file.png'

# code 
mymap = robjects.r.readRAST6(inputmap)
elev = rget("$")(rslot(mymap,"data"),"elevation.dem")
r_sorted = rsort(elev, decreasing=True)
p_seq = rseq(1,rlength(r_sorted))
x = rdiv(p_seq,rlength(r_sorted))
x = rdiv( ( rdiff( x, rmin(x) ) ) , ( rdiff( rmax(x) , rmin(x) ) ) )
y = rdiv( ( rdiff( r_sorted, rmin(r_sorted) ) ) , ( rdiff( rmax(r_sorted) , rmin(r_sorted) ) ) )
rpng(output_image)

result = df(x,y)
rplot(result,type = 'l',x)
------------------------------------------------------------
   File "<ipython console>", line 1
SyntaxError: non-keyword arg after keyword arg (<ipython console>, line 1)

my "rplot" actions seems to be wrong :-(

while if i try :

args = [x, y]
kwargs = {'ylab':"foo/bar", 'type':"b", 'col':"blue", 'log':"x"}
robjects.r.plot(*args, **kwargs)

i have a png file, but to "see the file" on my desktop
i had to type "twice" the command :
robjects.r.plot(*args, **kwargs)



thanks!!!

Massimo.

Il giorno 22/gen/2010, alle ore 00.38, Barry Rowlingson ha scritto:
[[elided Yahoo spam]]