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

From: Patrick Connolly <p_connolly_at_slingshot.co.nz>
Date: Tue, 22 Jul 2008 08:47:52 +1200

On Mon, 21-Jul-2008 at 10:12PM +0900, Gundala Viswanath wrote:

|> 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"

If it were to redo, it would get the same result (depending on what your function does). Assuming your function is using a random number, you could use do and while to continue trying according to your if test.

HTH

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}          		 Great minds discuss ideas    
 _( Y )_  	  	        Middle minds discuss events 
(:_~*~_:) 	       		 Small minds discuss people  
 (_)-(_)  	                           ..... Anon
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

______________________________________________
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 - 20:52:20 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