使用 rpy2 访问名称中带有点的函数(例如“as.vector") [英] Accessing functions with a dot in their name (eg. "as.vector") using rpy2

查看:50
本文介绍了使用 rpy2 访问名称中带有点的函数(例如“as.vector")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rpy2 从 Python 中访问as.vector"R 函数.让我们说,为了简单起见,我想使用 rpy2(R 代码)做一些像这样简单的事情:

I am trying to access the "as.vector" R function from within Python, using rpy2. Let's say, for the sake of simplicity, that I want to do something as simple as this using rpy2 (R code):

x <- as.vector(c(1, 2, 3))

由于as.vector"在其名称中包含一个点,它不能直接作为 rpy2.robjects.r 的成员使用

Since "as.vector" contains a dot in its name, it is not directly available as a member of rpy2.robjects.r

根据文档,rpy2 用下划线替换了命名函数参数的点,但它似乎不适用于函数名称本身.我试过例如."as_vector", "asvector" 无济于事.

According to the documentation, rpy2 replaces dots by underscores for named function parameters, but it doesn't seem to work for the function name itself. I tried eg. "as_vector", "asvector" to no avail.

有什么想法吗?

推荐答案

使用 rpy2.robjects.r 接口获取对函数的引用.

Get a reference to the function using the rpy2.robjects.r interface.

因此,您可以执行以下操作:

So, you could do something like:

as_vector = robjects.r("as.vector")
vect = as_vector(r_vect)

这篇关于使用 rpy2 访问名称中带有点的函数(例如“as.vector")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆