Re: [Rd] capturing multiple warnings in tryCatch()

2021-12-02 Thread Fox, John
Dear Henrik, Simon, and Adrian, As it turns out Adrian's admisc::tryCatchWEM() *almost* does what I want, which is both to capture all messages and the result of the expression (rather than the visible representation of the result). I was easily able to modify tryCatchWEM() to return the result

Re: [Rd] capturing multiple warnings in tryCatch()

2021-12-02 Thread Henrik Bengtsson
Simon's suggestion with withCallingHandlers() is the correct way. Also, note that if you use tryCatch() to catch warnings, you're *interrupting* the evaluation of the expression of interest, e.g. > res <- tryCatch({ message("hey"); warning("boom"); message("there"); 42 }, > warning = function(w)

Re: [Rd] capturing multiple warnings in tryCatch()

2021-12-02 Thread Adrian Dușa
Dear John, I have a function in package admisc called tryCatchWEM (catches warnings, errors and messages): > tryCatchWEM(foo()) $warning [1] "warning1" "warning2" Hope this helps, Adrian On Thu, 2 Dec 2021 at 23:04, Fox, John wrote: > Dear R-devel list members, > > Is it possible to capture m

Re: [Rd] capturing multiple warnings in tryCatch()

2021-12-02 Thread Simon Urbanek
Adapted from demo(error.catching): > W=list() > withCallingHandlers(foo(), warning=function(w) { W <<- c(W, list(w)); > invokeRestart("muffleWarning") }) > str(W) List of 2 $ :List of 2 ..$ message: chr "warning 1" ..$ call : language foo() ..- attr(*, "class")= chr [1:3] "simpleWarnin

[Rd] capturing multiple warnings in tryCatch()

2021-12-02 Thread Fox, John
Dear R-devel list members, Is it possible to capture more than one warning message using tryCatch()? The answer may be in ?conditions, but, if it is, I can't locate it. For example, in the following only the first warning message is captured and reported: > foo <- function(){ + warning("warn