I have a function fun
that often produces warnings and occasionally throws errors. I am trying to use tryCatch
to log warnings and errors, as suggested in this answer. How can I simultaneously store the warnings and errors?
Here is a minimal setup:
# Function for warnings, errors.
fun <- function(i) {
# Print warnings as they come in.
options(warn = 1)
# Issue warning.
warning(paste("Warn.", i))
# Stop.
if(i == 3) { stop(paste("Err.", i)) }
# Restore warning default behaviour.
options(warn = 0)
}
Evaluating fun
with tryCatch
:
# Storage
warns = list()
errs = list()
# Try catch the function and log the warnings/ errors.
for (i in 1:4) {
tryCatch(fun(i),
warning = function(w) warns[[i]] <<- w,
error = function(e) errs[[i]] <<- e
)
}
However, the output shows that the error hasn't been stored.
warns
# [[1]]
# <simpleWarning in fun(i): Warn. 1>
#
# [[2]]
# <simpleWarning in fun(i): Warn. 2>
#
# [[3]]
# <simpleWarning in fun(i): Warn. 3>
#
# [[4]]
# <simpleWarning in fun(i): Warn. 4>
errs
# list()