Saturday, 15 February 2014

r - Skipping error in for-loop -



r - Skipping error in for-loop -

i doing loop generating 180 graphs 6000 x 180 matrix (1 graph per column), of info don't fit criteria , error:

"error in cut.default(x, breaks = bigbreak, include.lowest = t) 'breaks' not unique".

i fine error, want programme go on running loop, , give me list of columns made error (as variable containing column names maybe?).

here's 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]) ## function defined in programme dev.off() }

note: have found numerous posts trycatch , none of them worked me (or @ to the lowest degree couldn't apply function correctly). help file wasn't helpful well.

help appreciated. thanks.

one (dirty) way utilize trycatch empty function error handling. example, next code raises error , breaks loop :

for (i in 1:10) { print(i) if (i==7) stop("urgh, iphone in blender !") } [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 erreur : urgh, iphone in blender !

but can wrap instructions trycatch error handling function nothing, illustration :

for (i in 1:10) { trycatch({ print(i) if (i==7) stop("urgh, iphone in 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 think should @ to the lowest degree print error message know if bad happened while letting code go on run :

for (i in 1:10) { trycatch({ print(i) if (i==7) stop("urgh, iphone in 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, iphone in blender ! [1] 8 [1] 9 [1] 10

edit : apply trycatch in case :

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]) ## function defined in programme dev.off() }, error=function(e){cat("error :",conditionmessage(e), "\n")}) }

r for-loop

No comments:

Post a Comment