[R] Remove warnings.

Mark Lyman mark.lyman at atk.com
Tue Oct 14 17:28:29 CEST 2008


 <rkevinburton <at> charter.net> writes:

> 
> The problem is that I am interested in all the warnings but after they have 
been reported using the
> 'warnings()' command I am no longer interested in those warnings but similar 
warnings (of the same type)
> may occur with subsequent invocations and I am interested in these "new" 
warnings not the ones that have
> been reported already. If 'warnings()' cleared out the warnings then it would 
be ideal for me.
> 
> Thank you.
> 
> Kevin
> ---- hadley wickham <h.wickham <at> gmail.com> wrote: 
> > Why don't you just suppress the warning messages you are not interested in?
> > 
> > ?suppressWarnings
> > 
> > Hadley
> > 
> > 
> > On Tue, Oct 14, 2008 at 7:59 AM,  <rkevinburton <at> charter.net> wrote:
> > > I have a function that could possibly generate warnings in a loop. What I 
want is to report the warnings
> (warnings()) then clear out the last.warning object so that if there is a 
call without warnings I will not
> see the previous warning.
> > > Some example code:
> > >
> > > generatewarning <- function(s)
> > > {
> > >        warning(s)
> > > }
> > >
> > > loop <- function(f=TRUE)
> > > {
> > >    if(f)
> > >    {
> > >        for(.index in 1:10)
> > >        {
> > >                if(.index %% 2)
> > >                {
> > >                        generatewarning(sprintf("%d warning", .index))
> > >                }
> > >        }
> > >     }
> > > }
> > >
> > > loop()
> > > warnings("TRUE")
> > > loop(FALSE)
> > > warnings("FALSE")
> > > loop()
> > > warnings("TRUE")
> > >
> > > Notice that the call to "warnings("FALSE")" still reports the warnings 
from the previousely generated
> warnings. I want to "clear" this set since it has already been reported. Is 
there a way to do this?
> > >
> > > Thank you.
> > >
> > > Kevin
> > >

Does setting the warn option to 1 do what you want?

> options(warn=1)
> generatewarning <- function(s)
+ {
+ warning(s)
+ }
> 
> loop <- function(f=TRUE)
+ {
+     if(f)
+     {
+ for(.index in 1:10)
+ {
+      if(.index %% 2)
+      {
+           generatewarning(sprintf("%d warning", .index))
+      }
+ }
+      }
+ }
> 
> loop()
Warning in generatewarning(sprintf("%d warning", .index)) : 1 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 3 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 5 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 7 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 9 warning
> warnings("TRUE")
NULL
> loop(FALSE)
> warnings("FALSE")
NULL
> loop()
Warning in generatewarning(sprintf("%d warning", .index)) : 1 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 3 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 5 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 7 warning
Warning in generatewarning(sprintf("%d warning", .index)) : 9 warning
> warnings("TRUE")
NULL



More information about the R-help mailing list