Executing R script with Python via subprocess.call
Question: I have a R script which works fine on its own but I need it to be a part of
python script. So, when I run the python script the R script be executed
automatically. I use the below command; there is no error but the R script
output files are not created.

    
    
    import subprocess
    
    retcode = subprocess.call("C:/Program Files/R/R-3.2.2/bin/Rscript --vanilla T:/2012.R", shell=True)
    

Thank you so much in advance.


Answer: Simply place your string command in brackets and break string into separate
components as the first parameter of function expects a list of arguments, per
the [doc](https://docs.python.org/2/library/subprocess.html):

> subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
    
    
    import subprocess
    
    retcode = subprocess.call(['C:/Program Files/R/R-3.2.2/bin/Rscript', '--vanilla', 
                               'T:/2012.R'], shell=True)
    

Alternatively, break it up into multiple strings:

    
    
    command = 'C:/Program Files/R/R-3.2.2/bin/Rscript'
    arg = '--vanilla'
    path2script = 'T:/2012.R'
    
    retcode = subprocess.call([command, arg, path2script], shell=True)
    

