2

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.

sgibb
  • 25,396
  • 3
  • 68
  • 74
silly_penguin
  • 201
  • 2
  • 5
  • 1
    Most likely because the script does not have permission to write? Ah wait....Win7. – PascalVKooten Oct 01 '13 at 15:54
  • I don't use Windows7, but sounds as though the task is being run by the "wrong" user. Have a look in its properties to set the user it runs as, you want it to be the user associated with "/Users/abg" (presumably you?) – Sam Mason Oct 01 '13 at 16:03

1 Answers1

1

Try the following instead of your write.table command

f.nm <- paste0(output_dir, Sys.Date(), "_forward_rates.csv")

# break it down into finer steps
file.create(f.nm)
f <- file(f.nm, open="w") # or open="a" if appending

write.table(results_df, file = f, sep = ",", append=FALSE, row.names = FALSE, col.names=TRUE)

close(f)

If this fails, you can at least see if the file is being created or not. If it is not, then you know that the issue is more likely due to OS connections or permissions problems

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • As was pointed out very quickly by the community, this was not an R or R batch issue, this was a Windows issue. By changing my output_dir variable to the following: output_dir <- "//10.10.10.100/shared/abg/Daily Forward Rates/" the problem was resolved. Thanks for everyone's help. – silly_penguin Oct 01 '13 at 20:01