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.
By default, try will continue the loop even if there's an error, but will still show the error message. We can supress the error messages by using silent = TRUE . That works for our purposes above, but there may be times when you want to handle errors differently.
8.3 Ignoring conditions The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() . Ignore messages with suppressMessages() .
For Loops. A for loop is sometimes called a counting loop because they're often used to count up to a specific number or iterate over a predefined collection. The most common error is to put a semicolon at the end of the for statement. This separates the for statement from its code.
The next statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it. On encountering next, the R parser skips further evaluation and starts next iteration of the loop.
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")}) }
Here's a simple way
for (i in 1:10) { skip_to_next <- FALSE # Note that print(b) fails since b doesn't exist tryCatch(print(b), error = function(e) { skip_to_next <<- TRUE}) if(skip_to_next) { next } }
Note that the loop completes all 10 iterations, despite errors. You can obviously replace print(b)
with any code you want. You can also wrap many lines of code in {
and }
if you have more than one line of code inside the tryCatch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With