简体   繁体   English

使用rpy2运行R函数时出错

[英]Error when running R function with rpy2

I'm trying to use rpy2 to run the multi.split function from the questionr package. 我试图用rpy2要逃避的multi.split功能questionr包。

this is my code 这是我的代码

from rpy2 import robjects
from rpy2.robjects.packages import importr

questionr = importr(str('questionr'))

data = ["red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green"]
data_vector = robjects.StrVector(data)
multi_split = questionr.multi_split
data_table = multi_split(data_vector, split_char='/')

after the last line I'm getting the following error: 最后一行之后,出现以下错误:

RRuntimeError: Error in `colnames<-`(`*tmp*`, value = c("c(\"red/blue\",_\"green\",_\"red/green\",_\"blue/red\",_\"red/blue\",_\"green\",_.blue",  : 
 'names' attribute [4] must be the same length as the vector [3]

I think that it has something to do with the size of the vector that I'm sending because if I remove the last item 我认为这与我要发送的向量的大小有关,因为如果我删除了最后一个项目

data = ["red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue"]

and then run 然后运行

data_vector = robjects.StrVector(data)
multi_split = questionr.multi_split
data_table = multi_split(data_vector, split_char='/')

I get no error message. 我没有收到错误消息。 also if I change the "split_char' var, for example: 同样,如果我更改了“ split_char”变量,例如:

data_table = multi_split(data_vector, split_char='.')

I get no error message, no matter with size of an array I'm sending. 无论我发送的数组大小如何,我都不会收到错误消息。

I have tried to run the matching code directly in R (with R-Studio) it runs with not problems. 我试图直接在R中运行匹配的代码(使用R-Studio),但运行没有问题。 Any ideas on how can I solve this issue? 关于如何解决此问题的任何想法?

This seems to be because the function multi_split ( multi.split in the R package) is trying to use the string representation of the expression associated with the first argument ( "data_vector" here). 这似乎是因为函数multi_split (R包中的multi.split )试图使用与第一个参数(此处为"data_vector" )关联的表达式的字符串表示形式。

The signature of the R function is: R函数的签名是:

multi.split(var, split.char = "/", mnames = NULL)

and the he documentation for mnames is: 他关于mnames文档是:

names to give to the produced variabels. 产生的变种的名称。 If NULL, the name are computed from the original variable name and the answers. 如果为NULL,则根据原始变量名称和答案计算名称。

In the call multi_split(data_vector, split_char='/') the embedded R cannot see the variable name as this is a Python call and data_vector is an anonymous variable (only content, no variable name). 在调用multi_split(data_vector, split_char='/') ,嵌入式R无法看到变量名,因为这是Python调用,而data_vector是匿名变量(仅内容,无变量名)。

I though that you could specify mnames , but you checked and this not working (see comments below). 我虽然可以指定mnames ,但是您检查了mnames ,但不起作用(请参阅下面的评论)。 That's what the code seems to say: the line vname <- deparse(substitute(var)) is evaluated no matter mnames is specified or not: https://github.com/juba/questionr/blob/9cf09f3ffcd6c8df24182380f12d52b061c221ef/R/table.multi.R#L161 这就是代码似乎要说的:无论是否指定了mnames,都会评估行vname <- deparse(substitute(var))https : //github.com/juba/questionr/blob/9cf09f3ffcd6c8df24182380f12d52b061c221ef/R/table。多.R#L161

The alternative is to work out the use of an R expression. 另一种方法是计算R表达式的用法。 An older post should provide the necessary bits for that: What object to pass to R from rpy2? 一个较旧的帖子应该为此提供必要的信息: 哪个对象从rpy2传递给R?

A third possibility is to creatively mix Python-strings-as-R-code: 第三种可能性是创造性地将Python字符串作为R代码进行混合:

data = ["red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green", "red/green", "blue/red", "red/blue", "green"]
data_vector = robjects.StrVector(data)
# binding the R vector to a symbol in R's "GlobalEnv"
robjects.globalenv['mydata'] = data_vector
# the call is now in a Python string that is evaluated as R code
data_table = robjects.r("multi.split(data_vector, split_char='/')")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM