How to call python script from R with arguments

I have a python script that takes about 5 arguments (file name, 3 int values ​​and 2 float values). I need to call this python script from R. How can I do this. I am trying to use rPython but this does not allow me to pass an argument

library("rPython") python.load("python scriptname") 

I don't know how to pass arguments

from the command line, I run my python script as:

 python scriptname filename 10 20 0.1 5000 30 
+7
python r rpython
source share
2 answers

You can call the 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) 

Arguments that are passed as on the command line. You will need to use sys.argv in 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 will display "hello world"

 system('python test.py hello world', wait=FALSE) 
+12
source share

There is a small typo in the big previous answer. The correct code is as follows:

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

where wait FALSE (do not wait = Flaz or wait = False)

+6
source share

All Articles