Re: [Rd] capturing multiple warnings in tryCatch()
Interesting exchange of ideas. A feature that should be included soon in the codebase to help in some use-cases -- eg. handling thousand database connections on {L,W}AN. Il Gio 2 Dic 2021, 23:38 Fox, John ha scritto: > 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. > > Henrik: I was aware that tryCatch() doesn't return the final result of the > expression, and I was previously re-executing the expression to capture the > reult, but only getting the first warning message, along with the result. > > Thanks for responding to my question and providing viable solutions, > John > > On 2021-12-02, 5:19 PM, "Henrik Bengtsson" > wrote: > > 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) { message("Warning caught: ", > conditionMessage(w)); 3.14 }) > hey > Warning caught: boom > > res > [1] 3.14 > > Note how it never completes your expression. > > /Henrik > > On Thu, Dec 2, 2021 at 1:14 PM Simon Urbanek > wrote: > > > > > > 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] "simpleWarning" "warning" > "condition" > > $ :List of 2 > > ..$ message: chr "warning 2" > > ..$ call : language foo() > > ..- attr(*, "class")= chr [1:3] "simpleWarning" "warning" > "condition" > > > > Cheers, > > Simon > > > > > > > On Dec 3, 2021, at 10:02 AM, Fox, John wrote: > > > > > > 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("warning 1") > > > + warning("warning 2") > > > + } > > > > > >> foo() > > > Warning messages: > > > 1: In foo() : warning 1 > > > 2: In foo() : warning 2 > > > > > >> bar <- function(){ > > > + tryCatch(foo(), warning=function(w) print(w)) > > > + } > > > > > >> bar() > > > > > > > > > Is there a way to capture "warning 2" as well? > > > > > > Any help would be appreciated. > > > > > > John > > > > > > -- > > > John Fox, Professor Emeritus > > > McMaster University > > > Hamilton, Ontario, Canada > > > Web: http://socserv.mcmaster.ca/jfox/ > > > > > > > > > > > > __ > > > R-devel@r-project.org mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > > > > __ > > R-devel@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capturing multiple warnings in tryCatch()
Hello, I remembered having seen a function tryCatch.W.E and after an online search, found where. It was in a R-Help post and in demo(error.catching). The question by Marius Hofert [1] was answered, among others, by Martin Maechler [2] which included the function tryCatch.W.E. These posts refer to an old thread dated 2004 [3], with an answer by Luke Tierney [4]. The function withWarnings posted by Luke returns all warning messages in a list, as seen below. I repost the function to have this self contained. withWarnings <- function (expr) { warnings <- character() retval <- withCallingHandlers(expr, warning = function(ex) { warnings <<- c(warnings, conditionMessage(ex)) invokeRestart("muffleWarning") }) list(Value = retval, Warnings = warnings) } withWarnings(foo()) #$Value #[1] "warning 2" # #$Warnings #[1] "warning 1" "warning 2" Function tryCatch.W.E is now part of contributed package simsalapar [5], with credits to Marius and Martin given in its documentation. [1] https://stat.ethz.ch/pipermail/r-help/2010-December/262185.html [2] https://stat.ethz.ch/pipermail/r-help/2010-December/262626.html [3] https://stat.ethz.ch/pipermail/r-help/2004-June/052092.html [4] https://stat.ethz.ch/pipermail/r-help/2004-June/052132.html [5] https://CRAN.R-project.org/package=simsalapar Hope this helps, Rui Barradas Às 22:37 de 02/12/21, Fox, John escreveu: 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. Henrik: I was aware that tryCatch() doesn't return the final result of the expression, and I was previously re-executing the expression to capture the reult, but only getting the first warning message, along with the result. Thanks for responding to my question and providing viable solutions, John On 2021-12-02, 5:19 PM, "Henrik Bengtsson" wrote: 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) { message("Warning caught: ", conditionMessage(w)); 3.14 }) hey Warning caught: boom > res [1] 3.14 Note how it never completes your expression. /Henrik On Thu, Dec 2, 2021 at 1:14 PM Simon Urbanek wrote: > > > 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] "simpleWarning" "warning" "condition" > $ :List of 2 > ..$ message: chr "warning 2" > ..$ call : language foo() > ..- attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition" > > Cheers, > Simon > > > > On Dec 3, 2021, at 10:02 AM, Fox, John wrote: > > > > 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("warning 1") > > + warning("warning 2") > > + } > > > >> foo() > > Warning messages: > > 1: In foo() : warning 1 > > 2: In foo() : warning 2 > > > >> bar <- function(){ > > + tryCatch(foo(), warning=function(w) print(w)) > > + } > > > >> bar() > > > > > > Is there a way to capture "warning 2" as well? > > > > Any help would be appreciated. > > > > John > > > > -- > > John Fox, Professor Emeritus > > McMaster University > > Hamilton, Ontario, Canada > > Web: http://socserv.mcmaster.ca/jfox/ > > > > > > > > __ > > R-devel@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capturing multiple warnings in tryCatch()
On Fri, 3 Dec 2021 at 00:37, Fox, John wrote: > 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. > Glad it helps. I would be happy to improve the function, should you send a reprex with the desired final result. Best wishes, Adrian [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capturing multiple warnings in tryCatch()
Dear Rui, Thanks for this. Simon referred to demo(error.catching), and Adrian's version returns the printed representation of the result along with messages. Best, John On 2021-12-03, 11:35 AM, "Rui Barradas" wrote: Hello, I remembered having seen a function tryCatch.W.E and after an online search, found where. It was in a R-Help post and in demo(error.catching). The question by Marius Hofert [1] was answered, among others, by Martin Maechler [2] which included the function tryCatch.W.E. These posts refer to an old thread dated 2004 [3], with an answer by Luke Tierney [4]. The function withWarnings posted by Luke returns all warning messages in a list, as seen below. I repost the function to have this self contained. withWarnings <- function (expr) { warnings <- character() retval <- withCallingHandlers(expr, warning = function(ex) { warnings <<- c(warnings, conditionMessage(ex)) invokeRestart("muffleWarning") }) list(Value = retval, Warnings = warnings) } withWarnings(foo()) #$Value #[1] "warning 2" # #$Warnings #[1] "warning 1" "warning 2" Function tryCatch.W.E is now part of contributed package simsalapar [5], with credits to Marius and Martin given in its documentation. [1] https://stat.ethz.ch/pipermail/r-help/2010-December/262185.html [2] https://stat.ethz.ch/pipermail/r-help/2010-December/262626.html [3] https://stat.ethz.ch/pipermail/r-help/2004-June/052092.html [4] https://stat.ethz.ch/pipermail/r-help/2004-June/052132.html [5] https://CRAN.R-project.org/package=simsalapar Hope this helps, Rui Barradas Às 22:37 de 02/12/21, Fox, John escreveu: > 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. > > Henrik: I was aware that tryCatch() doesn't return the final result of the expression, and I was previously re-executing the expression to capture the reult, but only getting the first warning message, along with the result. > > Thanks for responding to my question and providing viable solutions, > John > > On 2021-12-02, 5:19 PM, "Henrik Bengtsson" wrote: > > 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) { message("Warning caught: ", conditionMessage(w)); 3.14 }) > hey > Warning caught: boom > > res > [1] 3.14 > > Note how it never completes your expression. > > /Henrik > > On Thu, Dec 2, 2021 at 1:14 PM Simon Urbanek > wrote: > > > > > > 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] "simpleWarning" "warning" "condition" > > $ :List of 2 > > ..$ message: chr "warning 2" > > ..$ call : language foo() > > ..- attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition" > > > > Cheers, > > Simon > > > > > > > On Dec 3, 2021, at 10:02 AM, Fox, John wrote: > > > > > > 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("warning 1") > > > + warning("warning 2") > > > + } > > > > > >> foo() > > > Warning messages: > > > 1: In foo() : warning 1 > > > 2: In foo() : warning 2 > > > > > >> bar <- function(){ > > > + tryCatch(foo(), warning=function(w) print(w)) > > > + } > > > > > >> bar() > > > > > > > > > Is there a way to capture "warning 2" as well? > > > > > > Any help would be appreciated. > > > > > > John > > > >
Re: [Rd] capturing multiple warnings in tryCatch()
Dear Adrian, Here's my slightly modified version of your function, which serves my purpose: --- snip --- tryCatchWEM <- function (expr, capture = TRUE) { toreturn <- list() output <- withVisible(withCallingHandlers( tryCatch(expr, error = function(e) { toreturn$error <<- e$message NULL }), warning = function(w) { toreturn$warning <<- c(toreturn$warning, w$message) invokeRestart("muffleWarning") }, message = function(m) { toreturn$message <<- paste(toreturn$message, m$message, sep = "") invokeRestart("muffleMessage") })) if (capture & output$visible) { if (!is.null(output$value)) { toreturn$result <- output$value } } if (length(toreturn) > 0) { return(toreturn) } } --- snip --- The two small modifications are to change the default of capture to TRUE and to return output$value rather than capture.output(output$value). So a suggestion would be to modify the capture argument to, say, capture=c("no", "output", "value") and then something like . . . capture <- match.arg(capture) . . . if (capture == "output"){ toreturn$output <- capture.output(output$value) } else if (capture == "value"){ toreturn$value <- output$value } . . . Best, John On 2021-12-03, 1:56 PM, "R-devel on behalf of Adrian Dușa" wrote: On Fri, 3 Dec 2021 at 00:37, Fox, John wrote: > 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. > Glad it helps. I would be happy to improve the function, should you send a reprex with the desired final result. Best wishes, Adrian [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capturing multiple warnings in tryCatch()
Dear John, The logical argument capture is already in production use by other packages, but I think this is easily solved by: if (!is.null(output$value) & output$visible) { if (capture) { toreturn$output <- capture.output(output$value) } toreturn$value <- output$value } so that value is always part of the return list, if visible. This is a very good suggestion, and I've already incorporated it into this function. All the best, Adrian On Fri, 3 Dec 2021 at 21:42, Fox, John wrote: > Dear Adrian, > > Here's my slightly modified version of your function, which serves my > purpose: > > --- snip --- > > tryCatchWEM <- function (expr, capture = TRUE) { > toreturn <- list() > output <- withVisible(withCallingHandlers( > tryCatch(expr, > error = function(e) { > toreturn$error <<- e$message > NULL > }), warning = function(w) { > toreturn$warning <<- c(toreturn$warning, w$message) > invokeRestart("muffleWarning") > }, message = function(m) { > toreturn$message <<- paste(toreturn$message, > m$message, > sep = "") > invokeRestart("muffleMessage") > })) > if (capture & output$visible) { > if (!is.null(output$value)) { > toreturn$result <- output$value > } > } > if (length(toreturn) > 0) { > return(toreturn) > } > } > > --- snip --- > > The two small modifications are to change the default of capture to TRUE > and to return output$value rather than capture.output(output$value). So a > suggestion would be to modify the capture argument to, say, capture=c("no", > "output", "value") and then something like > > . . . > capture <- match.arg(capture) > . . . > if (capture == "output"){ > toreturn$output <- capture.output(output$value) > } else if (capture == "value"){ > toreturn$value <- output$value > } > . . . > > Best, > John > > On 2021-12-03, 1:56 PM, "R-devel on behalf of Adrian Dușa" < > r-devel-boun...@r-project.org on behalf of dusa.adr...@gmail.com> wrote: > > On Fri, 3 Dec 2021 at 00:37, Fox, John wrote: > > > 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. > > > > Glad it helps. > I would be happy to improve the function, should you send a reprex > with the > desired final result. > > Best wishes, > Adrian > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capturing multiple warnings in tryCatch()
Dear Adrian, For consistency, you might want to put toreturn$value <- output$value inside of if (capture) {}. In any event, it makes sense for me to wait for the modified admisc::tryCatchWEM to find its way to CRAN rather than to maintain my own version of the function. Thanks for this, John On 2021-12-03, 6:27 PM, "R-devel on behalf of Adrian Dușa" wrote: Dear John, The logical argument capture is already in production use by other packages, but I think this is easily solved by: if (!is.null(output$value) & output$visible) { if (capture) { toreturn$output <- capture.output(output$value) } toreturn$value <- output$value } so that value is always part of the return list, if visible. This is a very good suggestion, and I've already incorporated it into this function. All the best, Adrian On Fri, 3 Dec 2021 at 21:42, Fox, John wrote: > Dear Adrian, > > Here's my slightly modified version of your function, which serves my > purpose: > > --- snip --- > > tryCatchWEM <- function (expr, capture = TRUE) { > toreturn <- list() > output <- withVisible(withCallingHandlers( > tryCatch(expr, > error = function(e) { > toreturn$error <<- e$message > NULL > }), warning = function(w) { > toreturn$warning <<- c(toreturn$warning, w$message) > invokeRestart("muffleWarning") > }, message = function(m) { > toreturn$message <<- paste(toreturn$message, > m$message, > sep = "") > invokeRestart("muffleMessage") > })) > if (capture & output$visible) { > if (!is.null(output$value)) { > toreturn$result <- output$value > } > } > if (length(toreturn) > 0) { > return(toreturn) > } > } > > --- snip --- > > The two small modifications are to change the default of capture to TRUE > and to return output$value rather than capture.output(output$value). So a > suggestion would be to modify the capture argument to, say, capture=c("no", > "output", "value") and then something like > > . . . > capture <- match.arg(capture) > . . . > if (capture == "output"){ > toreturn$output <- capture.output(output$value) > } else if (capture == "value"){ > toreturn$value <- output$value > } > . . . > > Best, > John > > On 2021-12-03, 1:56 PM, "R-devel on behalf of Adrian Dușa" < > r-devel-boun...@r-project.org on behalf of dusa.adr...@gmail.com> wrote: > > On Fri, 3 Dec 2021 at 00:37, Fox, John wrote: > > > 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. > > > > Glad it helps. > I would be happy to improve the function, should you send a reprex > with the > desired final result. > > Best wishes, > Adrian > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel