[R] warnings details

Martin Morgan mtmorgan at fhcrc.org
Sat Oct 24 15:38:35 CEST 2009

Erich Neuwirth wrote:
> I am running R as an invisible subprocess in another program (RExcel).
> Using try I can catch errors and print the errors produced by an R
> statement.
> Is there a way to know if running a statement caused a warning message?
> last.warning gives me the last warning, but I do not have any indication
> what the statement was that caused the message.
> I can of course store last warning before I run
> a statement and then compare last.warning with this stored message
> to see if it has changed, but that is
> a) clumsy
> b) still does not cover situations when I get the same warning
> as the last one by coincidence.

?withCallingHandlers, perhaps with conditionCall, conditionMessage, and
suppressWarnings ?

> x <- suppressWarnings(withCallingHandlers({
+     y <- log(-1:1)
+     sqrt(y)
+ }, warning=function(warn) {
+     cat("my warning:\n  ")
+     print(warn)
+ }))
my warning:
  <simpleWarning in log(-1:1): NaNs produced>
my warning:
  <simpleWarning in sqrt(y): NaNs produced>
> x
[1] NaN NaN   0

Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

More information about the R-help mailing list