【问题标题】:How to input arguments from R to Python with rPython?如何使用 rPython 将参数从 R 输入到 Python?
【发布时间】:2017-08-06 23:31:34
【问题描述】:

如何使用包 rPython 从 R 向 Python 输入参数?

下面是一个幼稚的尝试

在 script.py 中:

print message

在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)

【问题讨论】:

    标签: python r parameter-passing rpython


    【解决方案1】:

    根据包的 docs(第 5-6 页)load 方法:

    此函数运行文件中包含的 Python 代码。通常,这 文件将包含要通过 python.call 或其他调用的函数 这个包中的函数。

    因此,在 Python 中没有运行脚本,而只定义了带有返回对象的函数:

    Python (script.py)

    def print_message(msg):
       return msg
    

    R (使用加载和调用)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2018-05-04
      相关资源
      最近更新 更多