Skip to content
Prev 305820 / 398506 Next

Trap an error from a function

On Wed, Sep 19, 2012 at 12:56 PM, David Winsemius
<dwinsemius at comcast.net> wrote:
I think it's easier to use tryCatch(), eg


sample.size <- tryCatch(

power.t.test(delta=14.02528,sd=1.945226,power=0.8,sig.level=0.05)$n,
                 error=function(e) NA
                 )

The first line gets run, and if there's an error, the error is passed
to the error= argument.  This is a function that takes the error as an
argument, and could do sophisticated things with it, but actually just
returns NA for all errors.

tryCatch() is also quieter.

  -thomas