Here's one approach: try_default <- function(expr, default = NA, quiet = FALSE) { result <- default if (quiet) { tryCatch(result <- expr, error = function(e) {}) } else { try(result <- expr) } result }
failwith <- function(default = NULL, f, quiet = FALSE) { function(...) try_default(f(...), default, quiet = quiet) } sd2 <- failwith(NA, sd) sd2(NA, na.rm=T) sd3 <- failwith(NA, sd, quiet = T) sd3(NA, na.rm=T) Hadley -- http://had.co.nz/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.