I use tryCatch
to handle warnings and errors in order to write a log file and secure my code. It write the log correctly with cat
when tryCatch
returns a success or an error but my problem is the loop execution is stopped when a warning is encountered.
Whitout the tryCatch
the loop is not interrupted by warning()
so it is not clear for me what's happening here because warning should not beaks loop.
Breaking loop when "warnings()" appear in R
Loop not interrupted :
>list <- c(1:10)
for(i in list){
warning(i)}
Warning: 1
Warning: 2
Warning: 3
Warning: 4
Warning: 5
Warning: 6
Warning: 7
Warning: 8
Warning: 9
Warning: 10
>
Loop interrupted :
>tryCatch(
{
list <- c(1:10)
for(i in list){
warning(i)
}
},
error=function(cond) {
message("ERROR: ", cond)
},
warning=function(cond) {
message("WARNING: ", cond)
}
)
WARNING: simpleWarning in doTryCatch(return(expr), name, parentenv, handler): 1
>
Thank you,