在for循环中跳过错误 [英] Skipping error in for-loop

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

问题描述

我正在做一个for循环,为我的6000 X 180矩阵(每列1个图)生成180个图表,其中一些数据不符合我的标准,我得到错误:

 在cut.default中的错误(x,breaks = bigbreak,include.lowest = T)
'breaks'不是唯一的。

我没有错,我想让程序继续运行for循环,并给我列出了什么列造成这个错误(作为一个变量包含列名可能?)。

这是我的命令:

($ 2 $ 180){
mypath = file.path(C:,file1,(paste(graph,names(mydata (列表名称),.pdf,sep = - )))
pdf(file = mypath)
mytitle = paste(anything)
myplotfunction(mydata [,columnnumber ])##这个函数之前在程序中定义
dev.off()
}

注意:我发现了许多关于tryCatch的文章,没有一篇适合我(或者至少我不能正确应用)。帮助文件也不是很有帮助。



帮助将不胜感激。谢谢。

解决方案

一个(脏)的方法是使用 tryCatch
pre $ for(i in 1:10){
打印(i)
if(i == 7)stop(哦,这个iphone在搅拌机里!)
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
Erreur :哦,iPhone是在搅拌机!

但是你可以把你的指令包装成 tryCatch

  for(i in 1:10){
tryCatch({
print(i)
if(i == 7)stop(Urgh,iphone is in the blender!)
},error = function(e){} )
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

但是我认为你应该至少打印错误信息,以便知道在让代码继续运行的过程中发生了什么事情:



$ $ p $ for(i in 1:10){
tryCatch({
print(i)
if(i = =)7)停止(哦,iPhone在搅拌机!)
},错误=功能(e){猫(错误:,conditionMessage(e),\ n)})

$ b $ [1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
错误:呃,iphone在搅拌机里!
[1] 8
[1] 9
[1] 10



$ {
mypath = file.path(C:,file1,(paste( (mydata),.pdf,sep = - )))
pdf myplotfunction(mydata [,columnnumber])##此函数在程序
dev.off()
}中定义,error = function(e){cat(ERROR:,conditionMessage(e ),\\\
)})
}


I am doing a for loop for generating 180 graphs for my 6000 X 180 matrix (1 graph per column), some of the data don't fit my criteria and i get the error:

"Error in cut.default(x, breaks = bigbreak, include.lowest = T) 
'breaks' are not unique". 

I am fine with the error, I want the program to continue running the for loop, and give me a list of what columns made this error (as a variable containing column names maybe?).

Here's my command:

for (v in 2:180){
    mypath=file.path("C:", "file1", (paste("graph",names(mydata[columnname]), ".pdf", sep="-")))
    pdf(file=mypath)
    mytitle = paste("anything")
    myplotfunction(mydata[,columnnumber]) ## this function is defined previously in the program
    dev.off()
}

Note: I have found numerous posts about tryCatch and none of them worked for me (or at least i couldn't apply the function correctly). The help file wasn't very helpful as well.

Help would be appreciated. Thanks.

解决方案

One (dirty) way to do it is to use tryCatch with an empty function for error handling. For example, the following code raises an error and breaks the loop :

for (i in 1:10) {
    print(i)
    if (i==7) stop("Urgh, the iphone is in the blender !")
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
Erreur : Urgh, the iphone is in the blender !

But you can wrap your instructions into a tryCatch with an error handling function that does nothing, for example :

for (i in 1:10) {
  tryCatch({
    print(i)
    if (i==7) stop("Urgh, the iphone is in the blender !")
  }, error=function(e){})
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

But I think you should at least print the error message to know if something bad happened while letting your code continue to run :

for (i in 1:10) {
  tryCatch({
    print(i)
    if (i==7) stop("Urgh, the iphone is in the blender !")
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
ERROR : Urgh, the iphone is in the blender ! 
[1] 8
[1] 9
[1] 10

EDIT : So to apply tryCatch in your case would be something like :

for (v in 2:180){
    tryCatch({
        mypath=file.path("C:", "file1", (paste("graph",names(mydata[columnname]), ".pdf", sep="-")))
        pdf(file=mypath)
        mytitle = paste("anything")
        myplotfunction(mydata[,columnnumber]) ## this function is defined previously in the program
        dev.off()
    }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}

这篇关于在for循环中跳过错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆