Reputation: 468
I have an R script that writes a dataframe. For simplicity's sake, let's say the dataframe is written as such in R:
testing = data.frame(x=c(1,2,3), y = c(4,5,6))
My goal is to use Python code to do some modeling on this dataframe (clearly not this actual dataframe, but again, keeping things simple). So I've written the following code in Python to source the R code in:
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import rpy2.robjects.packages as rpackages
base = importr('base')
utils = importr('utils')
from rpy2.robjects.lib.dplyr import DataFrame
from rpy2.robjects import pandas2ri
pandas2ri.activate()
...
r = robjects.r
r.source("python-testing_script.R")
The ellipses here represents me loading in other required packages, but they are unnecessary for this problem. Note python-testing_script.R
is where the r dataframe is being written.
I had thought that when sourcing in this R code, a Pandas dataframe would be written into my global environment in Python, but instead I get nothing new into the environment and the following output:
Out[1]:
R object with classes: ('list',) mapped to:
<ListVector - Python:0x00000143B9F98188 / R:0x00000143CD2BE988>
[DataFrame, BoolVector]
R object with classes: ('list',) mapped to:
<ListVector - Python:0x00000143B9F98188 / R:0x00000143CD2BE988>
[DataFrame, BoolVector]
visible: <class 'rpy2.robjects.vectors.BoolVector'>
R object with classes: ('logical',) mapped to:
<BoolVector - Python:0x00000143BFE14048 / R:0x00000143B9DC3DD8>
[ 0]
Any tips on what to do here? I could always write my dataframe to a csv and read the CSV file into Python, but I thought it would be more fun to have R and Python play nice together. I also realize that in this trivial case, I could just write the dataframe in Python, but my actual scenario is not so simple.
Upvotes: 2
Views: 374
Reputation: 468
I managed to figure it out. So the source
function is returning R objects, one of which is the dataframe I'm interested in. So all I had to do was tack this little guy on:
obj1, obj2 = r.source("python-testing_script.R")
testing = pandas2ri.ri2py(obj1)
This made the testing
dataframe from my R script into a pandas dataframe. Hope this can help someone else relatively new to Python out in the future.
Upvotes: 1