Take the 2-minute tour ×

I have an R script that makes a couple of plots. I would like to be able to execute this script from python.

I first tried:

import subprocess
subprocess.call("/.../plottingfile.R", shell=True)

This gives me the following error:

/bin/sh: /.../plottingfile.R: Permission denied

I do not know what the number 126 means. All my files are on the Desktop and thus I do not think that any special permissions would be needed? I thought that this error may have had something to do with cwd = none but I changed this and I still had an error.

Next I tried the following:

subprocess.Popen(["R --vanilla --args </.../plottingfile.R>"], shell = True)

But this too gave me an error with:

/bin/sh: Syntax error: end of file unexpected.

Most recently I tried:

subprocess.Popen("konsole | /.../plottingfile.R", shell = True)

This opened a new konsole window but no R script was ran. Also, I received the following error:



share|improve this question
does this script work when you run in from the command line w/o python? –  wespiserA Jul 14 '11 at 4:09
add comment

4 Answers

First thing first, make sure that you have your platttingfile.R script at a place where you can access. Typically it is the same directory.

I read in the internet that there is a utility that comes called RScript which is used to execute R script from the command line. So in order to run the script you would use python like this:

import subprocess
retcode = subprocess.call(['/path/to/RScript','/path/to/plottingfile.R'])

This would return the retcode 0 upon successful completion. If your plottingfile.R is returning some kind of an output, it will be thrown on STDOUT. If it pulling up some GUI, then it would come up.

If you want to capture stdout and stderr, you do it like this:

import subprocess
proc = subprocess.Popen(['/path/to/RScript','/path/to/plottingfile.R'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()

share|improve this answer
This worked for me. In the following link, please see a clever way for taking a normal command line and using split() on the string to create the exact array Popen is expecting. –  jatal Jun 19 '13 at 21:51
add comment

Shell error 126 is an execution error.

The permission denied implies that you have a "permission issue" specifically (duh haha)

Go the file and make sure R/Python is able to access it. I would try this out first:

$sudo chmod 777 /.../plottingfile.R

If the code runs, give it the correct but less accessible permission.

If this doesn't work, try changing R to Rscript.


share|improve this answer
add comment

have you tried chmod u+x /pathTo/Rscript.R ?

share|improve this answer
add comment

something likes this work usually for me:

subprocess.Popen("R --vanilla /PATH/plottingfile.R", shell = True)
share|improve this answer
add comment

Your Answer


