[R] handling warning messages

Henrik Bengtsson hb at maths.lth.se
Thu Mar 16 21:27:29 CET 2006


On 3/16/06, Martin Morgan <mtmorgan at fhcrc.org> wrote:
> also tryCatch
>
> > tryCatch( warning("a warning"),
> +          warning = function(w) {
> +            paste("Caught:", conditionMessage( w ))
> +          })
> [1] "Caught: a warning"
> >

BE CAREFUL!  When a condition, such as warnings and errors, are caught
by tryCatch() the expression that follows are NOT evaluated!  This is
probably not what you want/expected.

tryCatch({
  print(1)
  warning("a warning")
  print("We never get here!")
}, warning=function(w) {
  paste("Caught:", conditionMessage(w))
})

[1] 1
[1] "Caught: a warning"

/Henrik

>
>
> ronggui <ronggui.huang at gmail.com> writes:
>
> > see ?options
> >
> >  'warn': sets the handling of warning messages.  If 'warn' is
> >           negative all warnings are ignored.  If 'warn' is zero (the
> >           default) warnings are stored until the top-level function
> >           returns.  If fewer than 10 warnings were signalled they will
> >           be printed otherwise a message saying how many (max 50) were
> >           signalled.  A top-level variable called 'last.warning' is
> >           created and can be viewed through the function 'warnings'.
> >           If 'warn' is one, warnings are printed as they occur.  If
> >           'warn' is two or larger all warnings are turned into errors.
> >
> >      'warning.expression': an R code expression to be called if a
> >           warning is generated, replacing the standard message.  If
> >           non-null it is called irrespective of the value of option
> >           'warn'.
> >
> >      'warnings.length': sets the truncation limit for error and warning
> >           messages.  A non-negative integer, with allowed values
> >           100...8192, default 1000.
> >
> >
> > 2006/3/16, Sigal Blay <sblay at sfu.ca>:
> >> Is there any way to store the value of warnings but avoid printing them?
> >>
> >> A simplified example:
> >> > out <- c(0.2,0.3,0.4)
> >> > var <- c(2,3,4)
> >> > outcome <- glm(out ~ var, family=binomial())
> >> Warning message:
> >> non-integer #successes in a binomial glm! in: eval(expr, envir, enclos)
> >>
> >> I don't like the warning printed,
> >> but I would like to be able to check it's value after the top-level function
> >> has completed and than decide weather to print it or not.
> >>
> >> Thanks,
> >>
> >> Sigal Blay
> >> Statistical Genetics Working Group
> >> Department of Statistics and Actuarial Science
> >> Simon Fraser University
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >>
> >
> >
> > --
> > »ÆÈÙ¹ó
> > Deparment of Sociology
> > Fudan University
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>


--
Henrik Bengtsson
Mobile: +46 708 909208 (+1h UTC)




More information about the R-help mailing list