Skipping error in for-loop

前端 未结 3 1207
闹比i
闹比i 2020-11-27 10:39

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:



        
3条回答
  •  佛祖请我去吃肉
    2020-11-27 11:12

    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

提交回复
热议问题