使用rpy2将.R文件中的问题提交到python中

时间:2021-04-06 23:16:03

In rpy 1.x, to source in a .R file, all one had to do was this:

rpy 1。x,要在。r文件中找到源文件,我们要做的就是:

import rpy 
rpy.r.source("filename.R")

In rpy2, what needs to happen in order to source in a .R file from Python?

在rpy2中,从Python中获取.R文件需要发生什么?

I have tried several avenues, such as this:

我尝试过几种途径,比如:

import rpy2.robjects as ro
ro.source("filename.R")

Returns an error like this (sorry if this doesn't format correctly):

返回这样的错误(如果格式不正确,抱歉):

File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 166, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 99, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
RRuntimeError: Error in file(filename, "r", encoding = encoding) : 
cannot open the connection

Thanks in advance...

提前谢谢…

1 个解决方案

#1


3  

You should be able to run arbitrary R code with rpy2.robjects.r() (or .R(), don't think there's a difference):

您应该能够使用rpy2.robjects.r()(或.R()运行任意R代码,不认为有什么区别):

import rpy2.robjects as ro

ro.r("""source('filename.R')""")

I'd test filename.R first in RStudio (or your preferred alternative) first, just to make sure it's a valid file and can be sourced without issues.

我测试的文件名。R首先在RStudio中(或者您的首选方案),只是为了确保它是一个有效的文件,并且可以不受任何问题的影响。

#1


3  

You should be able to run arbitrary R code with rpy2.robjects.r() (or .R(), don't think there's a difference):

您应该能够使用rpy2.robjects.r()(或.R()运行任意R代码,不认为有什么区别):

import rpy2.robjects as ro

ro.r("""source('filename.R')""")

I'd test filename.R first in RStudio (or your preferred alternative) first, just to make sure it's a valid file and can be sourced without issues.

我测试的文件名。R首先在RStudio中(或者您的首选方案),只是为了确保它是一个有效的文件,并且可以不受任何问题的影响。