101

6000 X 180 マトリックス (列ごとに 1 つのグラフ) の 180 のグラフを生成するために for ループを実行していますが、データの一部が基準に適合せず、エラーが発生します。

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

エラーは問題ありません。プログラムで for ループの実行を続行し、このエラーが発生した列のリストを (列名を含む変数として) 表示するようにします。

これが私のコマンドです:

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()
}

注: tryCatch に関する多数の投稿を見つけましたが、どれも機能しませんでした (または、少なくとも関数を正しく適用できませんでした)。ヘルプ ファイルもあまり役に立ちませんでした。

助けていただければ幸いです。ありがとう。

4

3 に答える 3

175

これを行う 1 つの (汚い) 方法はtryCatch、エラー処理のために空の関数を使用することです。たとえば、次のコードではエラーが発生し、ループが中断されます。

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 !

ただし、tryCatch何もしないエラー処理関数で命令を にラップできます。たとえば、次のようになります。

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

しかし、少なくともエラー メッセージを表示して、コードの実行中に何か問題が発生したかどうかを確認する必要があると思います。

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

編集:あなたの場合に適用するtryCatchには、次のようになります:

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")})
}
于 2013-02-07T11:02:42.157 に答える
3

エラーをキャッチする代わりにmyplotfunction() 、エラーが発生するかどうか (つまり、ブレークが一意であるかどうか) を最初に関数内または関数の前でテストし、それが表示されない場合にのみプロットすることはできませんか?!

于 2013-02-07T11:13:20.107 に答える