我正在尝试自动化(在 Win7 系统上)一个 R 脚本以从 Bloomberg 读取数据并将其写入文件,以供另一个系统处理。我的代码完美地在 R gui 中运行。所以我写了一个批处理文件来调用这个.r文件并将结果输出到script.out,如下所示。当我双击批处理文件时,一切都成功运行。当我安排任务运行批处理文件时,R 代码运行,从 Bloomberg 收集数据,但每次写入文件都失败。
R代码
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)
批处理文件代码:
"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"
最后,这是 script.out 文件末尾的语句,似乎表明 write.table 命令失败。
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
我尝试将文件写入本地驱动器而不是网络驱动器,结果相同。
任何建议将不胜感激。