I would like to run an R script from python, and capture the output into a .Rout type file. The R script takes command line arguments. I am struggling... Minimal working example follows.
R script:
job_reference <- commandArgs(trailingOnly = TRUE)
arg1 <- job_reference[1]
arg2 <- job_reference[2]
arg3 <- job_reference[3]
arg4 <- job_reference[4]
print("***")
print(paste(arg1, arg2, arg3, arg4))
print("***")
python script:
import subprocess
arg1 = 'a'
arg2 = 'b'
arg3 = 'c'
arg4 = 'd'
subprocess.call(["/usr/bin/Rscript", "--no-save", "--no-restore", "--verbose", "print_letters.R", arg1, arg2, arg3, arg4, ">", "outputFile.Rout", "2>&1"])
The last line in the python script is via this stackoverflow link.
If I run the python script, the "outputFile.Rout" is not produced. However the python output includes:
running
'/usr/lib/R/bin/R --slave --no-restore --no-save --no-restore --file=print_letters.R --args a b c d > outputFile.Rout'
and when I run that from the command line without involving python the output file is produced. Note that I have tried specifying full paths to output files, etc.
Can anyone explain i) what is happening here; ii) how I can run an R script from python with the R script accepting command line arguments and producing an Rout type file?
Thanks in advance.