Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass convert string code of python to R using reticulate

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 ??

like image 773
Kush Patel Avatar asked Aug 28 '26 23:08

Kush Patel


1 Answers

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
like image 150
akrun Avatar answered Aug 30 '26 12:08

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!