3

I have function in python as following :

def print_hello():
    print("hello")

Rather than writing to python file and reading it from file using source_python. I would like just pass function as string and convert into R.

similar to following things

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

source_from_string(myPythonFunction)

print_hello()

Does reticulate package has this function ??

Kush Patel
  • 3,685
  • 5
  • 42
  • 65

1 Answers1

3

There is py_run_string to execute the module and the function 'print_hello' is called from the python env by py$

library(reticulate)
myPythonFunction <- "def print_hello():
                         print('hello')"
py_run_string(myPythonFunction)
py$print_hello()
#hello
akrun
  • 874,273
  • 37
  • 540
  • 662