简体   繁体   English

为什么 R 说没有循环中断/下一个,跳转到顶层

[英]Why does R say no loop for break/next, jumping to top level

Why does R throw the error "Error in value[3L]: no loop for break/next, jumping to top level" instead of going to the next iteration of a loop?为什么 R 会抛出错误“值 [3L] 中的错误:没有用于 break/next 的循环,跳转到顶层”而不是进入循环的下一次迭代? I'm on R version 2.13.1 (2011-07-08)我在 R 版本 2.13.1 (2011-07-08)

for (i in seq(10)) { 
   tryCatch(stop(), finally=print('whoops'), error=function(e) next) 
}

This problem came up because I wanted to create a different image or no image at all when plot failed.出现此问题是因为我想在 plot 失败时创建不同的图像或根本没有图像。 The code, using joran's approach, would look like this:使用 joran 的方法的代码如下所示:

for (i in c(1,2,Inf)) { 
   fname = paste(sep='', 'f', i, '.png')
   png(fname, width=1024, height=768) 
   rs <- tryCatch(plot(i), error=function(e) NULL)
   if (is.null(rs)){
    print("I'll create a different picture because of the error.")
   }
   else{
    print(paste('image', fname, 'created'))
    dev.off()
    next
   } 
}

Maybe you could try:也许你可以尝试:

for (i in seq(10)) { 
   flag <- TRUE
   tryCatch(stop(), finally=print('whoops'), error=function(e) flag<<-FALSE)
   if (!flag) next
}

Unfortunately, once you get inside your error function you're no longer in a loop.不幸的是,一旦您进入error function ,您就不再处于循环中。 There's a way you could hack around this:有一种方法可以解决这个问题:

for (i in seq(10)) { 
   delayedAssign("do.next", {next})
   tryCatch(stop(), finally=print('whoops'),
        error=function(e) force(do.next))
}

Though that is... well, hacky.虽然那是......好吧,hacky。 Perhaps there is a less hacky way, but I don't see one right off.也许有一种不那么老套的方法,但我看不到一个。

(This works because delayedAssign happens every loop, canceling out the efforts of force ) (这是有效的,因为每个循环delayedAssign发生延迟分配,从而抵消了force的努力)

EDIT编辑

Or you could use continuations:或者你可以使用延续:

for (i in seq(10)) { 
   callCC(function(do.next) {
       tryCatch(stop(), finally=print('whoops'),
           error=function(e)    do.next(NULL))
       # Rest of loop goes here
       print("Rest of loop")
   })
}

EDIT编辑

As Joris points out, you probably shouldn't actually use either of these, because they're confusing to read.正如 Joris 指出的那样,您可能实际上不应该使用其中任何一个,因为它们读起来很混乱。 But if you really want to call next in a loop, this is how:).但是,如果您真的想在循环中调用next ,这就是方法:)。

Wouldn't it make more sense to put the next outside the tryCatch based on an if check?根据if检查将next放在tryCatch之外不是更有意义吗? Something like this:像这样的东西:

for (i in c(1,2,Inf)) { 
   rs <- tryCatch(seq(i), finally=print('whoops'), error=function(e) NULL)
   if (is.null(rs)){
    print("I found an error!")
   }
   else{
    next
   } 
}

although I'm not sure this is what you want, since I'm a little unclear on what you're trying to do.虽然我不确定这是你想要的,因为我有点不清楚你想要做什么。

EDIT编辑

Based on the OP's revisions, this formulation works for me:根据 OP 的修订,这个公式对我有用:

plotFn <- function(fname,i){
    png(fname, width=400, height=200)
    plot(i)
    dev.off()
}


for (i in c(1,Inf,3)) { 
   fname = paste('f', i, '.png',sep="")
   rs <- tryCatch(plotFn(fname,i), error=function(e){dev.off(); return(NULL)})
   if (is.null(rs)){
    print("I'll create a different picture because of the error.")
   }
   else{
    print(paste('image', fname, 'created'))
    next
   } 
}

I'm certain that not having a dev.off() call in the case of an error needed to be fixed.我确定在需要修复错误的情况下没有dev.off()调用。 I'd have to dig a little deeper to figure out exactly why separating png and plot was causing problems.我必须更深入地了解为什么分离pngplot会导致问题。 But I think it's probably cleaner to keep the png(); plot(); dev.off()但我认为保留png(); plot(); dev.off()可能更干净。 png(); plot(); dev.off() png(); plot(); dev.off() sequence self contained anyway.无论如何, png(); plot(); dev.off()序列都是自包含的。 Also note that I put a dev.off() in the error function.另请注意,我在错误 function 中添加了dev.off()

I haven't tested what will happen if plotFn throws an error on png() , never creates the device and then reaches the error function and calls dev.off() .我还没有测试过如果plotFnpng()上引发错误、从不创建设备然后到达错误 function 并调用dev.off()会发生什么。 Behavior may depend on what else you have going on in your R session.行为可能取决于您在 R session 中还发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM