[R] error options

Paul Murtaugh murtaugh at science.oregonstate.edu
Wed Dec 28 20:35:15 CET 2011


Hi,

I'm running simulations that include a function that occasionally fails
because of an unpredictable singularity in a matrix that it tries to invert.
I'd like to have the function return 'NA' when that happens, so that the
simulations can continue.  I've tried things like:

    test <- function() {
    options(error=return(NA))
    x <- solve(0)
    return(x)
    }

which does return 'NA'.  But it returns 'NA' whether or not there's an
error in the function:

    test <- function() {
    options(error=return(NA))
    x <- 0
    return(x)
    }

    test()
    [1] NA

What am I missing here?

Thanks in advance
-Paul



More information about the R-help mailing list