我有一个python脚本,我想使用reticulate包从R中运行它。我想先在R中赋值一些变量,然后将它们传递给脚本。不幸的是,当我运行脚本时,我惊讶地发现python不能识别变量。这里我漏掉了什么?谢谢
Python脚本(test.py):
print(x)R代码:
library(reticulate)
x <- 5
source_python(test.py)错误:
Error in py_run_file_impl(file, local, convert) :
NameError: name 'x' is not defined发布于 2018-09-28 01:26:57
我想出的解决方案就是创建一个函数。所以如果之前我的python代码是
z = x + 3我的新python代码将是:
def add_three(x):
z = x + 3
return z然后我可以,在R运行中:
x <- 5
source_python("test.py")
y <- add_three(x)并得到y为6。
https://stackoverflow.com/questions/52526092
复制相似问题