[R] Flushing (Reset) 'last.warning'

Duncan Murdoch murdoch at stats.uwo.ca
Thu Dec 13 12:48:00 CET 2007


Tiago R Magalhães wrote:
> Dear members of the mailing list,
>
> I want to fetch warnings() from a series of prop.test calls. I want to
> get "none" if no warning is issued, and "warning" if there is a problem.
>
> I have looked (and relooked) at options(warn) and warning(), warnings()
> and 'last.warning' but to no avail. I am a biologist, so the "R language
> areas" are hard to fully understand.
>
> I cannot flush the warnings before each prop.test calls; hence if there
> is no warning, instead of "" I get (obviously) the last.warning from the
> previous call. I tried to remove 'last.warning' but it won't let me
> (wisely…) touch the base environment.
>
> Code below. And if anyone could give some pointers it would be great.
>
> Thank you, kind list
> ###############
> collect <- vector('character', 3)
> vec.1 <- c(10,19); total.1 <- c(24,35)
> vec.2 <- c(5,12); total.2 <- c(50,211)
>
> prop.test(vec.1, total.1); collect[1] <- names(warnings())
> prop.test(vec.2, total.2); collect[2] <- names(warnings())
> prop.test(vec.1, total.1); collect[3] <- names(warnings())
>
> collect
> [1] ""
> [2] "Chi-squared approximation may be incorrect"
> [3] "Chi-squared approximation may be incorrect"
>
> I wanted
> [1] ""
> [2] "Chi-squared approximation may be incorrect"
> [3] ""
>   
One workaround:  when you want to clear the old warning, issue a blank 
one.  For example:

warning("")
prop.test(vec.1, total.1); collect[1] <- names(warnings())
warning("")
prop.test(vec.2, total.2); collect[2] <- names(warnings())
warning("")
prop.test(vec.1, total.1); collect[3] <- names(warnings())

This puts a lot of spurious lines like

Warning message:
 
into your output; you might prefer to use text like "Resetting warning 
message" instead of a blank.

Duncan Murdoch



More information about the R-help mailing list