bob0901
bob0901

Reputation: 65

Newbie with Reticulate: How can I take the object from this python script to use in R?

Python Script

#!/bin/python3


import pandas as pd 
import numpy as np

class test(object):
    def checker(self):
        df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                       columns=['a', 'b', 'c'])
        return df2

if __name__ == "__main__":
    q = test()
    q.checker()

I want that df2 object. The dataframe.

R code

x <- py_run_file("new1.py")

The output ends of being a Dictionary with 28 items.

What is the correct way to grab that object in R using Reticulate?

Upvotes: 2

Views: 117

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

You need to pull an object from that environment:

import pandas as pd 
import numpy as np

class test(object):
    def checker(self):
        df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                       columns=['a', 'b', 'c'])
        return df2

if __name__ == "__main__":
    q = test()
    x = q.checker()

In R:

library(reticulate)
x <- py_run_file("test.py")$x

x
  a b c
1 1 2 3
2 4 5 6
3 7 8 9

Upvotes: 1

Related Questions