From j|ox @end|ng |rom mcm@@ter@c@  Fri Dec  3 20:42:34 2021
From: j|ox @end|ng |rom mcm@@ter@c@ (Fox, John)
Date: Fri, 3 Dec 2021 19:42:34 +0000
Subject: [Rd] capturing multiple warnings in tryCatch()
In-Reply-To: <20482_1638557779_1B3IuJTH009857_CAJ=0CtAVpv6QKAZwWxEB3fsjUD8PA6+-nBYnok=Kffa_SfNF3Q@mail.gmail.com>
References: <E3B708C1-8E1D-41A0-842A-9C05D229B3DB@mcmaster.ca>
 <516D77A0-7503-4695-A45F-1162ED816FCF@R-project.org>
 <CAFDcVCQuFVXJFn812gbLcDcY=gBeKwwXm8AQsW5Pm+wqpTzRoQ@mail.gmail.com>
 <8B6460C6-78A9-414E-8353-A817CD7A5292@mcmaster.ca>
 <20482_1638557779_1B3IuJTH009857_CAJ=0CtAVpv6QKAZwWxEB3fsjUD8PA6+-nBYnok=Kffa_SfNF3Q@mail.gmail.com>
Message-ID: <D3316D5B-283C-4FE0-ADF5-07C7F821872A@mcmaster.ca>

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-bounces at r-project.org on behalf of dusa.adrian at gmail.com> wrote:

    On Fri, 3 Dec 2021 at 00:37, Fox, John <jfox at mcmaster.ca> 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 at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] capturing multiple warnings in tryCatch()
Fox, John