【发布时间】:2017-12-26 13:15:34
【问题描述】:
我正在尝试循环浏览符号列表并下载数据并将其保存到 csv 文件。个别股票如果存在则工作得很好,但如果出现错误它会停止并且我不知道如何处理错误(R 的新手)我在这里使用了部分答案,但我无法找到错误处理的答案同时循环它以将其保存到文件中。
quantmod omitting tickers in getSymbols
startDate = Sys.Date()- 365
pth = "C:\\"
tickers <- c("LMT","AAPL","AMT", "GOOG")
#the sapply method works by not stopping when it has issues with LMT and still it goes not to dwld AAPL,
library(quantmod)
WoW <- new.env()
sapply(tickers, function(x){
try(
getSymbols(
x,
src ="google",
from =startDate,
env=WoW),
silent=TRUE)
})
#Now for the looping to save to file, somehow it does not go althe way till GOOG. it stops at AAPL
#Error in data.frame(sym) : row names contain missing values.
for (i in 1:length(tickers) ) {
col <- c( "Open","High","Low","Close","Volume")
sym <- eval(parse(text=paste("WoW$",tickers[i],sep="")))
if (!is.null(nrow(sym))){
colnames(sym) <- col
sym <- data.frame(sym)
sym <- cbind(BizDay = 0, sym)
sym$BizDay <- rownames(sym)
op <- paste0(pth,tickers[i],".csv")
print(op)
write.table(sym, file=op, na="", sep=",", row.names = FALSE)
}
}
关于如何处理基本错误的任何指示?我必须浏览完整的安全列表,并且必须确保我处理了这些。但现在坚持这一点。
谢谢
【问题讨论】: