我目前有一个R脚本,循环2000次(使用for
循环),每次循环使用url链接从数据库查询数据,并使用read.csv
函数将数据放入变量中。
我的问题是:当我查询少量数据时(约10000行),每次循环需要大约12秒,这很好。但现在我需要每个循环查询约50000行数据,查询时间会大大增加,每个循环约需要50秒左右。对我来说这还好,但有时我注意到服务器发送数据的时间更长(≈75-90秒),显然连接超时并出现以下错误:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open: HTTP status was '0 (nil)'
或者这个:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : InternetOpenUrl failed: 'The operation timed out'
我不会每次都得到相同的警告,它们之间会变化。
现在,我想避免程序在发生这种情况时停止运行,或者简单地防止此超时错误,并告诉R等待更长时间以获取数据。我已经尝试了在脚本开头使用以下设置作为可能的解决方案,但仍然会发生错误。
options(timeout=190)
setInternet2(use=NA)
setInternet2(use=FALSE)
setInternet2(use=NA)
任何其他建议或解决方法?也许是在发生此情况时跳到下一个循环,并将出现此错误的循环次数存储在变量中,以便最终可以再次查询,但仅针对由于连接错误而被跳过的循环中的那些
i
?理想的解决方案当然是避免出现此错误。
RCurl
。我看了Laszlo的解决方案,以为你在使用那个包。 - Richie Cottondownload.file
和另一个调用read.csv
。 - Richie Cotton