Answers for "next element in a loop if error in r"

R
0

next element in a loop if error in r

# This solution still prints the error but doesn't stop the loop
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
Posted by: Guest on September-17-2021

Browse Popular Code Answers by Language