R, command line, write to file fails

陌路散爱 提交于 2020-01-04 15:31:36

问题


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.


回答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



来源:https://stackoverflow.com/questions/19120450/r-command-line-write-to-file-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!