[R] error options

Paul Murtaugh
Wed, 28 Dec 2011 19:35:15 -0000

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