I am trying to use this Python script to call an r script and run it. The r script is dbc2csv.r, whose code is below the Python block.
I managed to call the r script and R studio opens but then the code does not run automatically as I would like it to happen. My feeling is that there is something wrong in the subprocess.call
line but I can't figure out what.
import subprocess
import os
os.chdir("path")
def dbc2csv(raw_filename):
dbc2csv_path = "Code\\R\\dbc2csv.R " + "Data\\CNES\\2005" + " " + "Data\\CNES\\2005" + " " + raw_filename
try:
r_script_path = subprocess.getoutput('Code\\R\\dbc2csv.r')
subprocess.call([r_script_path, "--vanilla", dbc2csv_path])
return True
except:
print("dbc2csv - Error converting file: " + raw_filename)
return False
dbc2csv("Data\\CNES\\STAC0508.dbc")
This is the r script I am trying to call and execute.
#install.packages("read.dbc") You need this package
library("read.dbc")
dbc2dbf <- function(rawDir, convertedDir, file) {
# reads dbc file
x <- read.dbc(paste(rawDir, file, sep=""))
# write it to csv
write.csv(x, file=paste(convertedDir, file, ".csv", sep=""))
}
args = commandArgs(trailingOnly=TRUE)
try(dbc2dbf(args[1], args[2], args[3]), TRUE)