2

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.

8
  • I try it, but still no result with no error message. Commented Mar 2, 2016 at 23:46
  • 2
    What happens with a simple test program: print.hw <- function() { print("hello world") }; print.hw() Commented Mar 3, 2016 at 0:50
  • Is there a syntax error with the path to the Rscript interpreter? My intuition is that you need to escape any space characters in the path. C:/Program\ Files/R/R-3.2.2/bin/Rscript Commented Mar 3, 2016 at 0:53
  • Does exact command run in Command Prompt or PowerShell: C:/Program Files/R/R-3.2.2/bin/Rscript --vanilla T:/2012.R? Commented Mar 3, 2016 at 2:22
  • 1
    Code.. code..code. Stop wasting our time vague error descriptions. Commented Mar 3, 2016 at 17:22

1 Answer 1

5

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:

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.