Search code examples
pythonrparameter-passingrpython

How to input arguments from R to Python with rPython?


How to input arguments from R to Python with the package rPython?

Below is a naive attempt

In script.py:

print message

In script.R:

require(rPython)    
text = "Hello World"
python.load("script.py", message=text)

Error in python.load("script.py", message = text) : 
  unused argument (message = text)

Solution

  • Per the package's docs (page 5-6) for the load method:

    This function runs Python code contained in a file. Typically, this file would contain functions to be called via python.call or other functions in this package.

    Hence, do not have a running script in Python but only defined functions with return objects:

    Python (script.py)

    def print_message(msg):
       return msg
    

    R (using load and call)

    require(rPython)    
    text = "Hello World"
    
    python.load("script.py")
    python.call("print_message", text)
    
    #[1] "Hello World"