I would like to use function for the accepted answer from this question:
How do I save warnings and errors as output from a function?
factory <- function(fun)
function(...) {
warn <- err <- NULL
res <- withCallingHandlers(
tryCatch(fun(...), error=function(e) {
err <<- conditionMessage(e)
NULL
}), warning=function(w) {
warn <<- append(warn, conditionMessage(w))
invokeRestart("muffleWarning")
})
list(res, warn=warn, err=err)
}
for use with the following function:
rbindlistfun <- function(df1, df2) {
x <- rbindlist(list(df1, df2), fill=TRUE, use.names=TRUE)
return (x)
}
The answer to the OP gives the following example:
test <- function(i)
switch(i, "1"=stop("oops"), "2"={ warning("hmm"); i }, i)
res <- lapply(1:3, factory(test))
But I do not completely understand how this relates to my function. How do I use the factory function with my example?