中跳到下一个循环值错误

中跳到下一个循环值错误

本文介绍了在R [tryCatch]中跳到下一个循环值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于 tryCatch 和cuzzins以及文档的其他一些问题:

I've read a few other SO questions about tryCatch and cuzzins, as well as the documentation:






  • Exception handling in R
  • catching an error and then branching logic
  • How can I check whether a function call results in a warning?
  • Problems with Plots in Loop

但我还是不明白。

我正在运行一个循环,想跳到下一个如果发生任何一种错误:

I'm running a loop and want to skip to next if any of a few kinds of errors occur:

for (i in 1:39487) {
  #ERROR HANDLING
  this.could.go.wrong <- tryCatch(
                           attemptsomething(),
                           error=function(e) next
                                  )
  so.could.this <- tryCatch(
                     doesthisfail(),
                     error=function(e) next
                            )

  catch.all.errors <- function() { this.could.go.wrong; so.could.this; }
  catch.all.errors;





  #REAL WORK
  useful(i); fun(i); good(i);

  }  #end for

(顺便说一下没有关于下一个的文档,我可以找到)

(by the way, there is no documentation for next that I can find)

当我运行这个,$ $ c> R honks:

When I run this, R honks:

Error in value[[3L]](cond) : no loop for break/next, jumping to top level

我在这里缺少什么基本点? tryCatch 显然位于循环的中,所以为什么不 R 知道吗?

What basic point am I missing here? The tryCatch's are clearly within the for loop, so why doesn't R know that?

推荐答案

使用 tryCatch 意识到它返回一个对象。如果 tryCatch 内有错误,则该对象将继承自错误类。您可以使用函数 inherit 来测试类继承。

The key to using tryCatch is realising that it returns an object. If there was an error inside the tryCatch then this object will inherit from class error. You can test for class inheritance with the function inherit.

x <- tryCatch(stop("Error"), error = function(e) e)
class(x)
"simpleError" "error"       "condition"

编辑:

参数的含义是什么 error = function(e)e ?这让我感到困惑,我不认为这在文档中有很好的解释。发生什么事情是这个参数会捕获您在 tryCatch ing的表达式中发生的任何错误消息。如果发现错误,则返回值为 tryCatch 。在帮助文档中,这被描述为一个调用处理程序 e 里面的 error = function(e)是您代码中出现的错误信息。

What is the meaning of the argument error = function(e) e? This baffled me, and I don't think it's well explained in the documentation. What happens is that this argument catches any error messages that originate in the expression that you are tryCatching. If an error is caught, it gets returned as the value of tryCatch. In the help documentation this is described as a calling handler. The argument e inside error=function(e) is the error message originating in your code.

我来自使用下一个的旧程序程序,这是一件坏事。所以我会重写你的代码这样的。 (请注意,我删除了 tryCatch 中的下一个语句。):

I come from the old school of procedural programming where using next was a bad thing. So I would rewrite your code something like this. (Note that I removed the next statement inside the tryCatch.):

for (i in 1:39487) {
  #ERROR HANDLING
  possibleError <- tryCatch(
      thing(),
      error=function(e) e
  )

  if(!inherits(possibleError, "error")){
    #REAL WORK
    useful(i); fun(i); good(i);
  }

}  #end for






下一个中的函数记录在中。

如果你想使用它,而不是把主要工作程序放在中,如果,你的代码应该是这样的:

If you want to use that instead of having your main working routine inside an if, your code should look something like this:

for (i in 1:39487) {
  #ERROR HANDLING
  possibleError <- tryCatch(
      thing(),
      error=function(e) e
  )

  if(inherits(possibleError, "error")) next

  #REAL WORK
  useful(i); fun(i); good(i);

}  #end for

这篇关于在R [tryCatch]中跳到下一个循环值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:50