Source problems in .R file in python using rpy2
In rpy 1.x, for the source in the .R file, all that had to be done was the following:
import rpy
rpy.r.source("filename.R")
In rpy2, what should happen to source in a .R file from Python?
I've tried several ways, for example:
import rpy2.robjects as ro
ro.source("filename.R")
Returns an error like this (sorry if this is not formatted 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...
+3
source to share
1 answer
You can run arbitrary R code with rpy2.robjects.r()
(or .R()
, don't think there is a difference):
import rpy2.robjects as ro
ro.r("""source('filename.R')""")
I tested filename.R
first in RStudio (or on your preferred alternative) first, just to make sure it is a valid file and can be sent without issue.
+3
source to share