Skip to content
Prev 305821 / 398506 Next

Trap an error from a function

Instead of "someClass" %in% class(someThing), as in
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