how to call python script from R with arguments

岁酱吖の 提交于 2019-12-03 05:27:00
badger0053

You can invoke a system command

system('python scriptname')

To run the script asynchronously you can set the wait flag to false.

system('python scriptname filename 10 20 0.1 5000 30', wait=FALSE)

The arguments that get passed as they would in command line. You will have to use sys.argv in the python code to access the variables

#test.py
import sys

arg1 = sys.argv[1]
arg2 = sys.argv[2]
print arg1, arg2

The R command below would output 'hello world'

system('python test.py hello world', wait=FALSE)

There is a small typo in the great previous answer. The right code is the following:

 system('python test.py hello world', wait = FALSE)

where wait is FALSE (not wait=Flase or wait=False)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!