0

I'm using subprocess to run a script.R file from test.py. My test.py goes as:

import subprocess
import pandas as pd
subprocess.call(["/usr/bin/Rscript", "--vanilla", "script.R"])   #line 3
df=pd.read_csv("output.csv")                                     #line 4

script.R goes as:

library(limma)
df <- read.csv("input.csv")
df<-normalizeCyclicLoess(df)
write.csv(df,"output.csv")

When I run the above file (test.py), I get an error:

FileNotFoundError: [Errno 2] File b'output.csv' does not exist: b'output.csv'

I understand that this error is because a output.csv file doesn't exist in my working directory. But, I assume that it would be created by script.R, which isn't happening probably because before the execution of line 3 finishes, python goes to line 4. We used help from here for this, and as mentioned, we are using call for this. What's going wrong then? Thanks...

EDIT: I noticed that if I don't import any libraries in my code (like limma above), everything works fine. I can write any lengthy code as I want, and it doesn't give me any error and proceeds to completion. But, as soon as I import any library, subprocess.call(....) gives me a non-zero result (zero means process completed). For a test, I changed my script.R to library(limma) (and nothing else) (tried for other libraries too- got the same results), and it gave me a non-zero result. Thus, I think, there's some problem with the import of libraries using subprocess. Please note that I'm able to run this R code directly, and nothing is wrong with my code/library. Please give me some hints on what might be going wrong here...

Ankit Kumar
  • 1,145
  • 9
  • 30
  • Your assumption seems to be incorrect. `subprocess.call()` is described in the docs: "Run the command described by args. Wait for command to complete.". – VPfB May 30 '19 at 08:26
  • @VPfB if that's the case, then why am I getting this error? Can you please give some hints? – Ankit Kumar May 30 '19 at 08:31
  • I'm sorry, I know Python, but not R. I would add some debugging print statements to see what is happening and when. – VPfB May 30 '19 at 08:52
  • @VPfB My R code works fine if I run directly – Ankit Kumar May 30 '19 at 10:37

1 Answers1

-1

Apologies, my initial answer was completely wrong.

Your subprocess call:

subprocess.call(["/usr/bin/Rscript", "--vanilla", "script.R"])

will return a number - this is the exit code of the process. If it succeeds, without error, it should be zero. You could check this to make sure your R code ran correctly.

Have you tried running your R code directly? Does it produce the output.csv? And if so, does it produce it in the right place?

PirateNinjas
  • 1,908
  • 1
  • 16
  • 21