problems sourcing in an .R file into python using rpy2

廉价感情. 提交于 2019-12-07 12:06:50

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25438097/problems-sourcing-in-an-r-file-into-python-using-rpy2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!