R,命令行,写文件失败 [英] R, command line, write to file fails

查看:3734
本文介绍了R,命令行,写文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动化(一个Win7系统上)的R脚本从彭博读取数据并将其写入由另一个系统对文件进行处理。我的code在R GUI运行完美。所以我写一个批处理文件来调用这个.R文件和结果输出到script.out如下图所示。当我双击该批处理文件,一切都运行成功。当我安排任务运行的批处理文件,则R code运行时,收集来自彭博社的数据,但在写文件失败每次。

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 $ C $ç

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)

批处理文件code:

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"

最后,这里是在这似乎表明write.table命令失败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.

任何建议将是极大的AP preciated。

Any suggestions would be greatly appreciated.

推荐答案

请尝试以下而不是你的 write.table 命令

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

这篇关于R,命令行,写文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆