Reticulate 新手:如何从这个 python 脚本中获取对象以在 R 中使用?

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

Python 脚本

#!/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()

我想要那个 df2 对象。数据框。

R码

x <- py_run_file("new1.py")

输出结束是一个包含 28 个项目的字典。

使用 Reticulate 在 R 中抓取该对象的正确方法是什么?

您需要从该环境中提取一个对象:

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()

在 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