Python 和 Rpy2:使用带有“.”的选项调用绘图函数在他们之中

发布于 2024-08-19 04:01:12 字数 318 浏览 10 评论 0原文

我刚刚开始学习如何将 rpy2 与 python 一起使用。我能够制作简单的绘图等,但我遇到了 R 中许多选项使用“.”的问题。例如,这是一个有效的 R 调用:

条形图(t,col=heat.colors(2),names.arg=c(“pwn”,“pwn2”))

其中 t 是矩阵。

我想在 python 中使用相同的调用,但它拒绝“。”名称.arg 的一部分。我的理解是,在Python中你替换“。”与“_”,例如名称_arg,但这也不起作用。我知道这是一个基本问题,所以我希望有人看到这个并知道解决方法。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2024-08-26 04:01:12

您可以在此处使用字典作为命名参数(使用 **)如文档中的描述,并直接调用 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})

(请注意,这是针对 rpy2 版本 2.0.x;未发布的 2.1 我还没有机会查看。)

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.)

一世旳自豪 2024-08-26 04:01:12

我不知道 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}

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:

>>> 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}
离不开的别离 2024-08-26 04:01:12

对于 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")))

必须使用 barplot_default (而不是 barplot)是由于
R 函数签名中广泛使用省略号“...”
事实上,保存参数名称翻译需要
分析函数包含的 R 代码。

更多,以及执行系统翻译的功能示例
的 '。'到“_”的位置是:
http://rpy.sourceforge.net/rpy2/doc- 2.1/html/robjects.html#functions

With rpy2-2.1.0, one way to write it would be:

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文