7

apparently try and trycatch do not work for this problem! Any alternative solutions?

i'm trying to make the following example code work without actually changing any code

result = 0
for(i in 1:10){
  result = result + i
  log("a") #I do not care about this error
  result = result + i
}

This should give result = 110

If i actually do it manually by copying the part inside the loop and increasing the counter it works perfectly:

result = 0

#iteration 1
i = 1
result = result + i
log("a")
result = result + i

#iteration 2
i = i+1
result = result + i
log("a")
result = result + i

#iteration 3
i = i+1
result = result + i
log("a")
result = result + i

#etc.

However my real code has about 1000 lines and needs to loop a few hundred times.

So i'd like to have some option

options(on.error.just.continue.the.next.line) = TRUE

I've read about try/tryCatch but I don't understand it correctly I think

2 Answers 2

11

If you just want the code to run, you can use try instead:

result <- 0
for(i in 1:10){
  result = result + i
  try({log("a")}) #I do not care about this error
  result = result + i
}

Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function
Error in log("a") : non-numeric argument to mathematical function

result
[1] 110

To turn off the message, use

try({log("a")}, silent=TRUE)

If you are worried about a larger block of code, you can wrap it in { } as follows:

result <- 0
for(i in 1:10){
  try({                # start code block
  result = result + i
  log("a")             # I do not care about this error
  result = result + i
  }, silent=TRUE)      # end of try function
}

result
[1] 55

Here, the first assignment to result completes in the for loop. Then the error occurs which "wipes out" the execution of the rest of the code block, which is the second assignment here. However, the loop execution is allowed to continue through completion.

Sign up to request clarification or add additional context in comments.

3 Comments

And what if about >100 parts of the code could possibly go wrong? I don't want to add try() around every line of code
Not a problem in some situations. I'll make an edit.
Ok, not really the solution I was looking for, I find it strange that there's no options to not wipe it out. Especially because it ignores it when I select it manually and run the block. But thanks
5

You can try using a try catch block:

result = 0
for (i in 1:10) {
    result = result + i
    tryCatch({
        log("a") #I do not care about this error
    }, warning = function(w) {
        # comment out the next print statement for a silent warning
        print("warning")
    }, error = function(e) {
        # comment out the next print statement for a silent error
        print("error")
    }, finally = {
        # cleanup
    }
    result = result + i
}

5 Comments

And what if about >100 parts of the code could possibly go wrong? I don't want to add try() around every line of code
I don't see anything wrong with using try in multiple places. This is routinely done in object oriented languages like Java, C++, and C#.
i've wrapped about hundred of lines in try, but my code still doesn't work. Do I literally need to wrap every line of code in try()? This looks as extremely ugly programming to me.
You might want to post your real code. As for your example use case, both answers given have you covered.
My code works fine outside a loop. I'm just looking for a way for it to continue within a loop as well as it does outside a loop. I don't understand why R behaves different when you add for(i in 1){ } around the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.