Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and Rpy2: Calling plot function with options that have "." in them

Tags:

python

rpy2

I'm just starting to learn how to use rpy2 with python. I'm able to make simple plots and such, but I've run into the problem that many options in R use ".". For example, here's an R call that works:

barplot(t, col=heat.colors(2), names.arg=c("pwn", "pwn2"))

where t is a matrix.

I want to use the same call in python, but it rejects the "." part of names.arg. My understanding was that in python you replace the "." with "_", so names_arg for example, but that is not working either. I know this is a basic problem so I hope someone has seen this and knows the fix. Thanks!

like image 720
Andy Hall Avatar asked Jan 23 '10 23:01

Andy Hall


1 Answers

You can use a dictionary here for the named arguments (using **) as described in the docs, and call R directly for the functions. Also remember that RPy2 expects its own vector objects. Yes, it's a bit awkward, but on the plus side, you should be able to do anything in rpy2 you could do in R.

from rpy2 import robjects
color = robjects.r("heat.colors")()
names = robjects.StrVector(("pwn", "pwn2"))
robjects.r.barplot(t, col=color, **{"names.arg":names})

(Note that this is for rpy2 version 2.0.x; there are some changes in the unreleased 2.1 which I haven't had a chance to look at yet.)

like image 84
Noah Avatar answered Oct 13 '22 00:10

Noah