I need to open an R script and provide it using input written in a separate python script. The subprocess module seems to be a good way to do this.
I came across some cryptic results, namely that I can write once and only once through p.stdin . Here is what I still have:
from subprocess import Popen, PIPE, STDOUT p = Popen(['r --no-save'],stdin=PIPE,stdout=PIPE,stderr=PIPE,shell=True) p.stdin.write("source('myrscript.R')\n") p.stdin.write('myfirstinput')
What happens when I run this code is that the first instance of stdin.write() executes as expected (and opens my R script), but the second line does nothing, and the subprocess (actually, R script) exits with an error, indicating that the subprocess did not receive any input, where the input was expected and, therefore, was terminated.
NB - In an ideal world, I would just interact directly through R, but this particular script requires complex resources that cannot be entered directly for practical purposes. In addition, rpy / rpy2 is not an option, as the end users of this script will not necessarily have access to this module or its dependencies. rscript also not an option (for many reasons, but mainly because of the variability in R end-user configurations).
Finally, p.communicate not an option, because apparently this will close the process after writing, and I will need to open it.
Thanks in advance
Jr
source share