Reputation: 2659
Best
I've a R-script, which is located on my amazon S3 Bucket.
What I want to do is, I would like to create a python 2.7 programm which can run R-scripts, (which are located and streamed via a S3-bucket).
To do this, i would like to use the rpy2 library (without downloading the script itself temp. (thus : s3.get_object()
unstead of s3.download_file()
)).
Main question:
how do I load, the R-script into R, without downloading it. because :
def __init__(self, r_script):
self._r = robjects.r
self._r.source(r_script.read())
or
def __init__(self, r_script):
self._r = robjects.r
self._r.source(r_script)
Doesn't work ... because it is not a location-string but already the code itself
Here is the content of my test script :
rm(list = ls())
test1<- function(){}
test2<- function(){}
test3<- function(){}
execute<- function(){
a <- c(1,2,3)
c <- c(1,2,3)
b <- c(1,2,3)
r <- data.frame(a,b,c)
return(r)
}
Kind regards
Upvotes: 1
Views: 165
Reputation: 11565
rpy2
can let you parse a string as R code before evaluating it, and this is used in an helper class making a Python namespace out of the string containing R code (See
Calling Custom functions from Python using rpy2).
from rpy2.robjects.packages import STAP
mymodule = STAP(r_script, "mymodule")
# call a function
mymodule.execute()
Note: I am seeing rm(list=ls)()
at the beginning of your R script. If your R script is meant to be a library it should not assume anything about the frame in which it is executed. To help with that, the class STAP
above will execute the R code in a new enviroment (not R's GlobalEnv
).
Upvotes: 2