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

From: Gundala Viswanath <gundalav_at_gmail.com>
Date: Mon, 21 Jul 2008 22:12:19 +0900


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:

__BEGIN__

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

       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
        }

__END__


R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 21 Jul 2008 - 13:15:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jul 2008 - 21:32:49 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive