R retriculate 直接从 R 代码中执行 python 代码

R retriculate execute python code direct from within R code

我正在尝试使用 retriculate 到 运行 python 来自 R 的代码。这有效,例如我可以 运行:

library(retriculate)
py_run_file("mypyfile.py")

其中 mypyfile.py 包含:

import pandas as pd
df = pd.DataFrame({a:[1,2], b:[3,4]})
df.to_csv(mypythongenerateddata.csv)

为了让事情更简单,我宁愿将这三行 python 代码直接放在 R 脚本中,而不是采购 python 脚本。文档似乎建议使用 markdownfile,我不喜欢它。 enter link description here

你可以使用 py_run_string:

library(reticulate)

py_run_string("
import pandas as pd
df = pd.DataFrame({'a':[1,2], 'b':[3,4]})
df.to_csv('mypythongenerateddata.csv')")

py$df
#>   a b
#> 1 1 3
#> 2 2 4