【发布时间】:2011-08-11 22:45:06
【问题描述】:
为什么 R 会抛出错误“值错误 [3L] : no loop for break/next, jumping to top level”而不是进入循环的下一次迭代?我在 R 版本 2.13.1 (2011-07-08)
for (i in seq(10)) {
tryCatch(stop(), finally=print('whoops'), error=function(e) next)
}
出现这个问题是因为我想在绘图失败时创建不同的图像或根本没有图像。使用 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
}
}
【问题讨论】:
-
对于这个问题的访问者试图找到一个更通用的答案来回答“为什么 R 说没有循环用于 break/next,跳转到顶层”,这个答案在这里做得很好:@ 987654321@
-
我觉得
eval(expr = next, envir = parent.frame(n = 1))或类似的解决方案应该可以工作,但我无法让它工作。也许其他人会知道。
标签: r