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...