Evaluate a possible erroneous expression without returning to prompt.
USAGE:
try(expr, silent = TRUE)
REQUIRED ARGUMENTS:
expr
An expression to evaluate. It will be evaluated in the frame of the
caller.
OPTIONAL ARGUMENTS:
silent
If
FALSE then print the error
message if there is an error evaluating
expr.
By default the error message is not printed. (It is returned
by the function if there is an error, so it will be printed if the result
is not assigned to a variable, just like the return value of any function.)
VALUE:
Either the value of the expression or an object of class "Error"
containing the error message generated if there was an error while
evaluating the expression.
SEE ALSO:
,
EXAMPLES:
# avoid stopping simulation because of bad data points
set.seed(1)
inputs <- sapply(1:10, function(i)rpois(1,1))
f <- function(x) if(x<1) stop("x<1") else log(x)
val <- lapply(inputs, function(x)try(f(x)))
unlist(val[!sapply(val, is, "Error")]) # extract ones that worked