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. -- Erich Neuwirth, University of Vienna Faculty of Computer Science Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-39464 Fax: +43-1-4277-39459
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. >You could clear the old message by issuing your own warning, e.g. warning("Cleared warnings in RExcel") save.warning <- last.warning ... do something ... if (!identical(last.warning, save.warning)) warnings() Working at a lower level, the R_WriteConsoleEx function tells a GUI whether it is writing a regular message or a warning/error, so you could keep track of that if you're working at that level. Duncan Murdoch
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 -- 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