[R] Trap an error from a function

William Dunlap wdunlap at tibco.com
Wed Sep 19 03:25:09 CEST 2012


Instead of "someClass" %in% class(someThing), as in
>  function(x)  if( "try-error" %in%
>                      class( try( test[test[x:1]] ) ) ){
>                      2}else{0}
it is better to use inherits(someThing, "someClass"), as in
   function(x) {
             res <- try( test[test[x:1]], silent=TRUE )
             if (inherits(res, "try-error")) { NA } else { res }}
(where I return the result of the attempted operation instead of 2
and NA for an error instead of 0.)

A related approach is to use tryCatch():
    function(x) tryCatch(test[test[x:1]], error=function(e)NA)
You can also catch warnings and messages with tryCatch.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of David Winsemius
> Sent: Tuesday, September 18, 2012 5:35 PM
> To: John Sorkin
> Cc: r-help at r-project.org
> Subject: Re: [R] Trap an error from a function
> 
> 
> On Sep 18, 2012, at 5:10 PM, John Sorkin wrote:
> 
> > Window 7
> > R 2.15
> >
> > I am writing a simulation which generates sample sized estimates from simulated data.
> When I run the function shown below,
> > power.t.test(delta=14.02528,sd=1.945226,power=0.8,sig.level=0.05)
> >
> > I get an error message:
> >
> >> power.t.test(delta=14.02528,sd=1.945226,power=0.8,sig.level=0.05)
> > Error in uniroot(function(n) eval(p.body) - power, c(2, 1e+07)) :
> >  f() values at end points not of opposite sign
> >
> > The fact that the function can not return a sample size is OK, however I need to trap
> the error and set the sample size equal to NA. How do I trap the error so that when the
> error occurs I can set sample size equal to NA?
> 
> ?conditions   #### has lots of fancy stuff
> # But I use just plain old `try`
> 
> test=-10:10
> sapply(test, function(x)  if( "try-error" %in%
>                      class( try( test[test[x:1]] ) ) ){
>                      2}else{0} )
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
> Error in test[x:1] : only 0's may be mixed with negative subscripts
>  [1] 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0
> 
> You still get the messages but the code runs.
> 
> --
> 
> David Winsemius, MD
> Alameda, CA, USA
> 
> ______________________________________________
> 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.




More information about the R-help mailing list