Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测到错误时,使用trycatch在下一个循环上运行_R - Fatal编程技术网

检测到错误时,使用trycatch在下一个循环上运行

检测到错误时,使用trycatch在下一个循环上运行,r,R,尝试编写trycatch函数以检测错误。但循环在检测到错误后不会继续运行 for(number in 1:10){ tryCatch({ user_link = suppressMessages(remoteDriver$findElement(using = 'css selector', x[number])) }, error = function(e){ user_link = NA}) user_link = as.character(user_l

尝试编写trycatch函数以检测错误。但循环在检测到错误后不会继续运行

for(number in 1:10){
    tryCatch({
      user_link = suppressMessages(remoteDriver$findElement(using = 'css selector', x[number]))
    }, error = function(e){ user_link = NA})

    user_link = as.character(user_link$getElementText())

    commentdf1 = data.frame(user_link)
    commentdf = rbind(commentdf,commentdf1)
  }

我希望在循环中运行时有10条信息。如果输入
user\u link=NA时出错,我建议尝试找到一种替代
tryCatch
的方法。它会使代码更难阅读,使意外错误几乎无法检测到,如果
tryCatch
中使用的任何包更改了代码,您的代码将停止工作,同时不会产生可识别的错误

也就是说,您的问题不在
tryCatch
函数中,而是在
user\u link$getElementText()
之后直接进行的调用中,如果
tryCatch
函数导致错误,则会输出不同的错误

for(number in 1:10){
    tryCatch({
      user_link = suppressMessages(remoteDriver$findElement(using = 'css selector', x[number]))
    }, error = function(e){ user_link = NA})

    user_link = as.character(user_link$getElementText())

    commentdf1 = data.frame(user_link)
    commentdf = rbind(commentdf,commentdf1)
  }
有几种方法可以解决这个问题。最简单的方法就是使用
next
操作符跳过迭代。在这种情况下,最好在
data.frame
中包含哪些迭代成功

for(number in 1:10){
    if(isFALSE(
        tryCatch({
        user_link = suppressMessages(
            remoteDriver$findElement(using = 'css selector', x[number])
        )}, error = function(e) FALSE)
        )
      ) next

    user_link = as.character(user_link$getElementText())

    #Include iteration so we can see which succeeded and which did not.
    commentdf1 = data.frame(user_link, iteration = i)
    commentdf = rbind(commentdf,commentdf1)
  }

除了包含迭代之外,您还可以先创建整个
data.frame
,然后使用
commentdf[i]输入成功的迭代,请发布错误消息。如果
user\u link
是NA:
as.character(user\u link$getElementText())
,那么接下来的这一行将出错。感谢您的指导,我现在就完成了。如果我检测到错误,我可以检查如何输入“NA”吗?两种简单的方法是:1)预创建
数据.frame
并插入行(例如
commentdf之类的内容),或者您可以grep错误消息,捕捉特定错误,并让其他意外错误抛出错误
tryCatch(stop('catch this'),error=function(e)if(grepl('catch',e$message))其他e)
@rawr,这不应该起作用,因为失败的原因应该来自对
$getElementText()的后续调用
。眼前的问题是@Geo希望将
NA
值放入他的
数据框中,这需要在代码本身中移动一点。我指的是
它会使代码更难阅读,产生几乎无法检测到的意外错误,以及在tryCatch中使用的任何包的情况下更改代码后,您的代码将停止工作,同时不会产生可识别的错误