使用 rpy2 将 .R 文件导入 python 的问题 [英] problems sourcing in an .R file into python using rpy2
问题描述
在 rpy 1.x 中,要在 .R 文件中获取源代码,您只需要做的就是:
<块引用>导入 rpyrpy.r.source("文件名.R")
在 rpy2 中,要从 Python 获取 .R 文件需要做什么?
我尝试了多种途径,例如:
<块引用>import rpy2.robjects as roro.source("文件名.R")
返回这样的错误(抱歉,如果格式不正确):
文件C:\Python27\lib\site-packages\rpy2\robjects\functions.py",第 166 行,在 __call__ 中return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)文件C:\Python27\lib\site-packages\rpy2\robjects\functions.py",第 99 行,在 __call__ 中res = super(Function, self).__call__(*new_args, **new_kwargs)RRuntimeError:文件错误(文件名,r",编码 = 编码):无法打开连接
提前致谢...
您应该能够使用rpy2.robjects.r()
(或.R()<)运行任意R代码
/code>,不要觉得有区别):
import rpy2.robjects as roro.r("""source('文件名.R')""")
我会首先在 RStudio(或您的首选替代方案)中测试 filename.R
,以确保它是一个有效文件并且可以毫无问题地获取.
In rpy 1.x, to source in a .R file, all one had to do was this:
import rpy rpy.r.source("filename.R")
In rpy2, what needs to happen in order to source in a .R file from Python?
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...
You should be able to run arbitrary R code with rpy2.robjects.r()
(or .R()
, don't think there's a difference):
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.
这篇关于使用 rpy2 将 .R 文件导入 python 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!