如何从 RStudio 中的 R 脚本调用 Python 变量

How do I call a Python variable from an R script in RStudio

我在 RStudio 环境中打开了两个脚本。一个 Python_script.py 和 r_script.R

我希望能够在任一脚本中保存一个变量,并能够从另一个脚本中以另一种语言调用该变量。

提前致谢

您可以使用 reticulate 包来完成:

Python_script.py(运行 在 R studio 中):

import numpy as np

x = np.random.rand(100)

quit

r_script.R:

library(reticulate)

py$x
#> [1] 0.144915024 0.824587306 0.781184497 0.442235857 0.848616639
#  [6] 0.474798959 0.426096485...

y <- 1:10

Python_script2.py:

r.y
# >>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

或者在 R

中 运行 reticulate::source_python('Python_script.py')