0

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?

Tom
  • 2,173
  • 1
  • 17
  • 44
  • I think there is an error in your `factory()` function. Do you really need that `function(...)` on the second line? – Ashwin Malshe Apr 18 '19 at 13:14
  • @AshwinMalshe I did not write the code, but I think it should be fine (I got it from another post, but accidentally linked it to the wrong post, it has the right link now). – Tom Apr 18 '19 at 13:37

0 Answers0