Re: [Rd] Capturing warnings with capture.output

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 05 Sep 2006 - 15:43:09 GMT

Modify withWarnings in:

   https://stat.ethz.ch/pipermail/r-help/2004-June/052132.html like this:

withWarnings <- function(expr) {

     wHandler <- function(w) {
      cat(w$message, "\n")
      invokeRestart("muffleWarning")
     }
     withCallingHandlers(expr, warning = wHandler)
}

# test
out <- capture.output(

   withWarnings({

      print(1)
      warning("A")
      print(2)
      warning("B")
      print(3)

   })
)

On 9/5/06, hadley wickham <h.wickham@gmail.com> wrote:
> > Something like this which displays the warnings and also writes
> > them to out so that they are captured:
>
> Is it possible to not display the warnings (just write them out) ?
>
> Hadley
>
>
>
>
> >
> > out <- capture.output(
> > withCallingHandlers({
> > print(1)
> > warning("A warning.")
> > print(2)
> > warning("Another warning.")
> > print(3)
> > }, warning = function(x) cat(x$message, "\n"))
> > )
> >
> > print(out)
> >
> >
> > On 8/29/06, hadley wickham <h.wickham@gmail.com> wrote:
> > > Is there any way to include warnings in the output from capture.output? eg:
> > >
> > > a <- capture.output(warning("test"))
> > > all.equal(a, "Warning message: \n test ")
> > >
> > > Conceptually, this seems like redirecting stderr to stdout, or somehow
> > > changing warning to simple print it's output. I've had a look at
> > > tryCatch and using a warning handler, but this terminates execution at
> > > the warning.
> > >
> > > Thanks,
> > >
> > > Hadley
> > >
> > > ______________________________________________
> > > 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 Received on Wed Sep 06 01:45:39 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 06 Sep 2006 - 11:39:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.