I am trying to automate (on a Win7 system) an R script to read data from Bloomberg and write it to file, for processing by another system. My code runs in the R gui perfectly. So I wrote a batch file to call this .r file and output results to script.out as shown below. When I double click the batch file everything runs successfully. When I schedule a task to run the batch file, the R code runs, collects data from Bloomberg, but the write to file fails every time.
R code
library(quantmod)
library(rJava)
library(Rbbg)
#Bloomberg connectivity
conn <- blpConnect()
#Initialize Java Virtual MAchine
.jinit(classpath="myClasses.jar", parameters="-Xmx512m")
setwd("C:/Users/abg/Skydrive/Documents/Bloomberg Historical Data 2013-07-30/data")
output_dir <- "W:/abg/Daily Forward Rates/"
results_df<- data.frame(Currency=character(), Spot=character(), sp_bid=numeric(), sp_ask=numeric(),
TN=character(), tn_bid=numeric(), tn_ask=numeric(),
SN=character(), sn_bid=numeric(), sn_ask=numeric(),stringsAsFactors=FALSE)
for(i in 1:length(list.files(pattern='*\\.csv')))
{
currency <- substr(list.files(pattern='*\\.csv')[i],1,6)
securities <- c(paste(currency, " Curncy", sep=""))
fields <- c("BID", "ASK", "TIME")
bb_results<-bdp(conn, securities, fields)
print(bb_results)
results_df[i,1]<-currency
results_df[i,2]<- "SPOT"
results_df[i,3]<- bb_results$BID
results_df[i,4]<- bb_results$ASK
}
results_df[is.na(results_df)]<-""
write.table(results_df, file = paste(output_dir, Sys.Date(), "_forward_rates.csv", sep=""), sep = ",", append=FALSE, row.names = FALSE, col.names=TRUE)
batch file code:
"C:\Program Files\R\R-3.0.1\bin\x64\R.exe" CMD BATCH --vanilla --slave "C:\Users\abg\SkyDrive\Documents\tom next rates from bloomberg.R" "C:\Users\abg\SkyDrive\Documents\script.out"
Finally, here is the statement at the end of the script.out file that seems to indicate the write.table command failed.
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Calls: write.table -> file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'W:/abg/Daily Forward Rates/2013-10-01_forward_rates.csv': No such file or directory
Execution halted
I've tried writing the file to a local drive instead of a network drive, same results.
Any suggestions would be greatly appreciated.