如何使用reticulate将Python字符串代码转换为R

3
我有一个Python函数如下:

def function_name(args):

def print_hello():
    print("hello")

不需要将内容写入Python文件并使用source_python从文件中读取。我想直接将函数作为字符串传递并转换为R语言。

类似于以下的方法:

library(reticulate)
myPythonFunction <- "def print_hello():
     print("hello")
"

source_from_string(myPythonFunction)

print_hello()

请问“reticulate”软件包是否有此功能?
1个回答

3

py_run_string用于执行模块,函数'print_hello'由py$从python环境中调用。

library(reticulate)
myPythonFunction <- "def print_hello():
                         print('hello')"
py_run_string(myPythonFunction)
py$print_hello()
#hello

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接