how to call python script from R with arguments

后端 未结 2 1628
自闭症患者
自闭症患者 2021-02-07 05:45

I have a python script which takes some 5 arguments( a filename, 3 int values and 2 float values). I need to call this python script from R. How can I do that. I am trying to us

相关标签:
2条回答
  • 2021-02-07 06:23

    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)

    0 讨论(0)
  • 2021-02-07 06:31

    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)
    
    0 讨论(0)
提交回复
热议问题