在R中的异常处理中分配值 [英] Assigning a value in exception handling in R
本文介绍了在R中的异常处理中分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
while(bo!=10){
x = tryCatch(getURLContent(Site, verbose = F, curl = handle),
error = function(e) {
cat("ERROR1: ", e$message, "\n")
Sys.sleep(1)
print("reconntecting...")
bo <- bo+1
print(bo)
})
print(bo)
if(bo==0) bo=10
}
我想尝试在连接失败后每秒重新连接一次。但是bo值的新分配无效。我怎样才能做到这一点?或者,如果您知道如何使用RCurl选项重新连接(我确实没有找到任何东西),那就太神奇了。
I wanted to try reconnecting each second after the connection failed. But the new assignment of the bo value is not effective. How can i do that? Or if you know how to reconnect using RCurl options (I really didn't find a thing) it would be amazing.
感谢所有帮助
推荐答案
问题是分配的 b0
范围。但是,我发现 try
比 tryCatch
更友好。这应该起作用:
The problem is the b0
scope of the assignment. However, I find try
a little more friendly than tryCatch
. This should work:
while(bo!=10){
x = try(getURLContent(Site, verbose = F, curl = handle),silent=TRUE)
if (class(x)=="try-error") {
cat("ERROR1: ", x, "\n")
Sys.sleep(1)
print("reconnecting...")
bo <- bo+1
print(bo)
} else {
break
}
}
以上尝试10次连接到现场。如果任何一次成功,它将退出。
The above attempts 10 times to connect to the site. If any of this time succeeds, it exits.
这篇关于在R中的异常处理中分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文