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
Below. -- Bert On Wed, Dec 28, 2011 at 11:35 AM, Paul Murtaugh <murtaugh at science.oregonstate.edu> wrote:> 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?error trapping. ?try ##or ?tryCatch -- Bert> > Thanks in advance > -Paul > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm