使用rpy2将.R文件中的源代码导入python(problems sourcing in an .R file into python using rpy2)

编程入门 行业动态 更新时间:2024-10-28 04:24:55
使用rpy2将.R文件中的源代码导入python(problems sourcing in an .R file into python using rpy2)

在rpy 1.x中,要获取.R文件,所有人必须做的就是:

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

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

我尝试了几种途径,例如:

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

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

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

提前致谢...

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

最满意答案

您应该能够使用rpy2.robjects.r() (或.R() 运行任意R代码 ,不要认为存在差异):

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

我首先在RStudio(或您首选的替代方案)中测试filename.R ,只是为了确保它是一个有效的文件,并且可以没有问题地获取。

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.

更多推荐

本文发布于:2023-08-06 17:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1452910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源代码   文件   python   file   sourcing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!