[R] How to catch warnings

Michael Bedward michael.bedward at gmail.com
Thu Nov 18 12:32:30 CET 2010


Like this ?

f <- function() {
  # on first warning will print to console and return results so far

  out <- c(1,2,3,4)
  extras <- list(good=c(2,3,4,5), bad=c(9,9,9), ugly=c(9,9,9,9,9))

  ok <- TRUE
  for (ex in extras) {
    tryCatch(out <- cbind(out, ex),
      warning=function(w) {
      	cat("problem values:", ex, "\n")
      	ok <<- FALSE
      }
    )
    if (!ok) break
  }

  out  	
}

Michael


On 18 November 2010 22:14, Alaios <alaios at yahoo.com> wrote:
>
> The best would be the R when a warning stop. To issue a warnign at console stop the execution at the point it was and let your write commands in console to check what was going on.
>
> Is that possible?
> Regards
> Alex
>
> --- On Thu, 11/18/10, Michael Bedward <michael.bedward at gmail.com> wrote:
>
> From: Michael Bedward <michael.bedward at gmail.com>
> Subject: Re: [R] How to catch warnings
> To: "Alaios" <alaios at yahoo.com>
> Cc: "Rhelp" <r-help at r-project.org>
> Date: Thursday, November 18, 2010, 10:17 AM
>
> Sorry Alex, I don't quite follow what you want.  What do you mean by
> "it would be nicer if the execution is paused so to try to print more
> values" ?
>
> Michael
>
> On 18 November 2010 21:11, Alaios <alaios at yahoo.com> wrote:
> >
> > Dear Michael,
> > I would like to thank you for your reply. This line made things easier. One more question what If I want to halt or pause the program when a warning happens? Right now I get only a message printed but it would be nicer if the execution is paused so to try to print more values.
> >
> > Best REgards
> >
> > Alex
> >
> > --- On Thu, 11/18/10, Michael Bedward <michael.bedward at gmail.com> wrote:
> >
> > From: Michael Bedward <michael.bedward at gmail.com>
> > Subject: Re: [R] How to catch warnings
> > To: "Alaios" <alaios at yahoo.com>
> > Cc: "Rhelp" <r-help at r-project.org>
> > Date: Thursday, November 18, 2010, 1:26 AM
> >
> > Hi Alex,
> >
> > Something like this ?
> >
> > x <- 1:4
> > y <- list(good=2:5, bad=3:5)
> > for (yy in y) {
> >    tryCatch( x <- cbind(x, yy),
> >        warning=function(w) cat("problem values: ", yy, "\n")
> >    )
> > }
> >
> > Michael
> >
> >
> > On 18 November 2010 03:19, Alaios <alaios at yahoo.com> wrote:
> > > Hello when my code executes I receive the message that were some warnings. I want to catch warning messages at run time so to print some local variables and try to understand why this warning happens.
> > >
> > > I searched on internet and I tried withCallingHandlers(
> > > which seems to work but as I used Rkward the result is awful. I get a prompt to copy paste a value only while at the same time my background window with my code is not anymore accessible.
> > >
> > > Actually I get the warning that
> > > In f[cbind(shweights[, 1], shweights[, 2])] * shweights[,  ... :
> > >   longer object length is not a multiple of shorter object length
> > >
> > > and what I want is when the warning appear to be able to print the value shweights[,1] shweights[,2] to see what is going on.
> > >
> > > Any easier way to do that?
> > > Best REgards
> > > Alex
> > >
> > >
> > >
> > >
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> > >
> >
>



More information about the R-help mailing list