### Name: try ### Title: Try an Expression Allowing Error Recovery ### Aliases: try ### Keywords: programming ### ** Examples ## this example will not work correctly in example(try), but ## it does work correctly if pasted in options(show.error.messages = FALSE) try(log("a")) print(.Last.value) options(show.error.messages = TRUE) ## alternatively, print(try(log("a"), TRUE)) ## run a simulation, keep only the results that worked. set.seed(123) x <- stats::rnorm(50) doit <- function(x) { x <- sample(x, replace=TRUE) if(length(unique(x)) > 30) mean(x) else stop("too few unique points") } ## alternative 1 res <- lapply(1:100, function(i) try(doit(x), TRUE)) ## alternative 2 ## Not run: ##D res <- vector("list", 100) ##D for(i in 1:100) res[[i]] <- try(doit(x), TRUE) ## End(Not run) unlist(res[sapply(res, function(x) !inherits(x, "try-error"))])