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:
我刚刚开始学习如何在python中使用rpy2。我可以画一些简单的图,但是我遇到了R使用的很多选项的问题。例如,这里有一个R调用可以工作:
barplot(t, col=heat.colors(2), names.arg=c("pwn", "pwn2"))
barplot(t,坳= heat.colors(2),名字。arg = c(“pwn”、“pwn2”))
where t is a matrix.
其中t是一个矩阵。
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!
我想在python中使用相同的调用,但它拒绝name .arg的“.”部分。我的理解是,在python中,您将“.”替换为“_”,例如names_arg,但这也不起作用。我知道这是一个基本的问题,所以我希望有人看到了这个问题并知道解决办法。谢谢!
3 个解决方案
#1
9
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.
您可以在这里使用字典来获取文档中描述的命名参数(使用**),并直接为函数调用R。还要记住RPy2期望它自己的向量对象。是的,这有点尴尬,但从积极的方面来说,你应该能够在rpy2中做任何你能在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.)
(注意,这是rpy2 2.0.x版本;在未发布的2.1中有一些变化,我还没有机会看到。
#2
1
I don't know whether Rpy will accept this, but you can have keyword parameters with periods in them. You have to pass them through a dictionary though. Like this:
我不知道Rpy是否会接受这一点,但是您可以使用带有句点的关键字参数。不过你得把它们通过字典。是这样的:
>>> def f(**kwds): print kwds
...
>>> f(a=5, b_c=6)
{'a': 5, 'b_c': 6}
>>> f(a=5, b.c=6)
Traceback ( File "<interactive input>", line 1
SyntaxError: keyword cant be an expression (<interactive input>, line 1)
>>> f(**{'a': 5, 'b.c': 6})
{'a': 5, 'b.c': 6}
#3
0
With rpy2-2.1.0, one way to write it would be:
使用rpy2-2.1.0,一种写法是:
from rpy2.robjects.packages import importr
graphics = importr("graphics")
grdevices = importr("grDevices")
graphics.barplot_default(t,
col = grdevices.heat_colors(2),
names_arg = StrVector(("pwn", "pwn2")))
Having to use barplot_default (rather that barplot) is due to the extensive use of the ellipsis '...' in R'sfunction signatures and to the fact that save parameter name translation would require analysis of the R code a function contains.
不得不使用barplot_default(而不是barplot)是因为省略的广泛使用……在R的函数签名和保存参数名称转换需要分析函数包含的R代码。
More, and an example of a function to perform systematic translation of '.' to '_' is at: http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#functions
还有一个函数的例子来进行系统的翻译。to“_”的地址是:http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#函数。
#1
9
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.
您可以在这里使用字典来获取文档中描述的命名参数(使用**),并直接为函数调用R。还要记住RPy2期望它自己的向量对象。是的,这有点尴尬,但从积极的方面来说,你应该能够在rpy2中做任何你能在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.)
(注意,这是rpy2 2.0.x版本;在未发布的2.1中有一些变化,我还没有机会看到。
#2
1
I don't know whether Rpy will accept this, but you can have keyword parameters with periods in them. You have to pass them through a dictionary though. Like this:
我不知道Rpy是否会接受这一点,但是您可以使用带有句点的关键字参数。不过你得把它们通过字典。是这样的:
>>> def f(**kwds): print kwds
...
>>> f(a=5, b_c=6)
{'a': 5, 'b_c': 6}
>>> f(a=5, b.c=6)
Traceback ( File "<interactive input>", line 1
SyntaxError: keyword cant be an expression (<interactive input>, line 1)
>>> f(**{'a': 5, 'b.c': 6})
{'a': 5, 'b.c': 6}
#3
0
With rpy2-2.1.0, one way to write it would be:
使用rpy2-2.1.0,一种写法是:
from rpy2.robjects.packages import importr
graphics = importr("graphics")
grdevices = importr("grDevices")
graphics.barplot_default(t,
col = grdevices.heat_colors(2),
names_arg = StrVector(("pwn", "pwn2")))
Having to use barplot_default (rather that barplot) is due to the extensive use of the ellipsis '...' in R'sfunction signatures and to the fact that save parameter name translation would require analysis of the R code a function contains.
不得不使用barplot_default(而不是barplot)是因为省略的广泛使用……在R的函数签名和保存参数名称转换需要分析函数包含的R代码。
More, and an example of a function to perform systematic translation of '.' to '_' is at: http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#functions
还有一个函数的例子来进行系统的翻译。to“_”的地址是:http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects.html#函数。