From j|ox @end|ng |rom mcm@@ter@c@  Sat Dec  4 02:40:39 2021
From: j|ox @end|ng |rom mcm@@ter@c@ (Fox, John)
Date: Sat, 4 Dec 2021 01:40:39 +0000
Subject: [Rd] capturing multiple warnings in tryCatch()
In-Reply-To: <8582_1638574041_1B3NRK7r032693_CAJ=0CtC6ZOpbxTBKhgWmovQ9L1f4LKir-z92ru-XtrxgFV5X2Q@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>
 <D3316D5B-283C-4FE0-ADF5-07C7F821872A@mcmaster.ca>
 <8582_1638574041_1B3NRK7r032693_CAJ=0CtC6ZOpbxTBKhgWmovQ9L1f4LKir-z92ru-XtrxgFV5X2Q@mail.gmail.com>
Message-ID: <4E0E55D8-250E-4781-BA8C-FBCEEFAD59BA@mcmaster.ca>

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" <r-devel-bounces at r-project.org on behalf of dusa.adrian at gmail.com> 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 <jfox at mcmaster.ca> 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-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
    >
    >

    	[[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