【发布时间】:2015-10-28 22:48:56
【问题描述】:
我目前正在使用此功能。我是 R 新手,所以不知道连接是如何工作的,而谷歌没有帮助的一件事。
我的代码从一个 crest api 获取我的数字。
df <- readWorksheetFromFile("marketTypeIDS.xls", sheet=1, startRow = 1, endCol = 2)
typeIDs <- unname(unlist(df[,1]))
calcday<-as.numeric(Sys.Date())-30
currentdate<-as.numeric(Sys.Date())
itemcount <- 0
monthvolumes <- seq(1:11897)
baseurl <- "https://public-crest.eveonline.com/market/10000048/types/"
monthlyvolume <- (0)
tmpvol <- (0)
for (i in 1:11897)
{
itemcount <- fromJSON(paste0(baseurl, typeIDs[i], "/history/"), flatten = TRUE)$totalCount
Sys.sleep(0.034)
if (itemcount ==0)
{
monthvolumes[i] <- 0
}
else
{
repeat
{
currentdate <- as.Date(fromJSON(paste0(baseurl, typeIDs[i], "/history/"), flatten = TRUE)$items[itemcount,8])
Sys.sleep(0.034)
if (as.numeric(currentdate)<calcday)
{
break
}
tmpvol <- fromJSON(paste0(baseurl, typeIDs[i], "/history/"), flatten = TRUE)$items[itemcount,6]
Sys.sleep(0.034)
monthlyvolume <- monthlyvolume+tmpvol
itemcount <- itemcount-1
if (itemcount==0)
{
break
}
}
monthvolumes[i]<-monthlyvolume
monthlyvolume<-0
}
}
它在 ~700 处停止(应该超过 11000 次)然后给我这个错误:
Error in open.connection(con, "rb") : Timeout was reached
6 open.connection(con, "rb")
5 open(con, "rb")
4 parse_con(txt, 1024^2, bigint_as_char)
3 parseJSON(txt, bigint_as_char)
2 fromJSON_string(txt = txt, simplifyVector = simplifyVector, simplifyDataFrame = simplifyDataFrame,
simplifyMatrix = simplifyMatrix, flatten = flatten, ...)
1 fromJSON(paste0(baseurl, typeIDs[i], "/history/"), flatten = TRUE)
In addition: Warning message:
closing unused connection 3 (https://public-crest.eveonline.com/market/10000048/types/18/history/)
此连接是在第一次运行 for 循环时创建的(它以链接中的 18 开头) 我怎么能事先关闭这个连接,这样它就不会破坏循环? (这只运行了大约一个小时,因此很难通过“尝试”进行测试)
提前感谢您的帮助! 如果你有任何其他建议,我的耳朵是开放的!
【问题讨论】:
-
因为你实际上遇到了一个错误,你可以在你的
fromJSON调用周围使用try或tryCatch(我会为fromJSON创建一个包装函数),捕获错误,用它做一些聪明的事情,然后脚本就会运行。或者,您可以使用httr::GET/POST,在那里设置超时值(以防这是服务器端的合法延迟),然后也使用try/tryCatch作为包装器。我们没有marketTypeIDS.xls来帮助测试,所以这可能是你能得到的最好的。 -
dropbox.com/s/xl8t343iwfv3sr5/eveQT.rar?dl=0 这里是完整的源代码和 xls 文档。你的回答很有道理。尽管我仍然想知道如何关闭此连接并且我想知道为什么在超时时一切都会中断。
标签: r