[R] Howto Restart A Function with Try-Error Catch

Gundala Viswanath gundalav at gmail.com
Mon Jul 21 15:12:19 CEST 2008

Hi all,

I have a function - let's call it "myfunction". This function is based
on some random
number generator. Now, once in a while the function will break/crash depending
on the random number it generate inside the function.

To avoid the problem, what I intend to do is the following:

1. Catch the try-error using class.
2.  Redo the function if it returns "try-error"
3.  Otherwise keep the output of the function.

I'm not sure how to create the above construct.
The code I have below doesn't work:


     myfunction <- function(the_x) {
        # do something
        a = list(output1=val1, output2 = val2)

       out <- try(suppressWarnings(myfunction(x)),silent=T)

        if (class(out) == "try-error") {
          #this clause doesn't seem to "redo"
          out <- myfunction(X)
        else {
         ll <- out$output1


- Gundala Viswanath
Jakarta - Indonesia

More information about the R-help mailing list