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

Patrick Connolly p_connolly at slingshot.co.nz
Mon Jul 21 22:47:52 CEST 2008


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
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list