From dmedr| @end|ng |rom gm@||@com  Fri Dec  3 10:19:43 2021
From: dmedr| @end|ng |rom gm@||@com (Daniele Medri)
Date: Fri, 3 Dec 2021 10:19:43 +0100
Subject: [Rd] capturing multiple warnings in tryCatch()
In-Reply-To: <8B6460C6-78A9-414E-8353-A817CD7A5292@mcmaster.ca>
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>
Message-ID: <CAOSBRp9UW-2jynwZtnNg5rjnD0XKoaXEkqtxVpB3tCCtVUi=cA@mail.gmail.com>

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 <jfox at mcmaster.ca> 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" <henrik.bengtsson at gmail.com>
> 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
>     <simon.urbanek at r-project.org> 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 <jfox at mcmaster.ca> 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()
>     > > <simpleWarning in foo(): warning 1>
>     > >
>     > > 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 at r-project.org mailing list
>     > > https://stat.ethz.ch/mailman/listinfo/r-devel
>     > >
>     >
>     > ______________________________________________
>     > R-devel at r-project.org mailing list
>     > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



[Rd] capturing multiple warnings in tryCatch()
Daniele Medri