[R] Display warning only once in session

William Dunlap wdunlap at tibco.com
Mon Aug 25 23:50:29 CEST 2014


You could use local() to associate a state variable with your function:
   myFunc <- local({
       notWarnedYet <- TRUE
       function(x) {
           if (notWarnedYet) {
               warning("myFunc is funky")
               notWarnedYet <<- FALSE # note use of <<-
           }
           sqrt(log2(x))
      }
   })
E.g.,
   > myFunc(1:5)
   [1] 0.000000 1.000000 1.258953 1.414214 1.523787
   Warning message:
   In myFunc(1:5) : myFunc is funky
   > myFunc(1:5)
   [1] 0.000000 1.000000 1.258953 1.414214 1.523787
   > myFunc(1:5)
   [1] 0.000000 1.000000 1.258953 1.414214 1.523787

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Aug 25, 2014 at 2:05 PM, Berry Boessenkool
<berryboessenkool at hotmail.com> wrote:
>
> Hi,
>
> I'm writing a function that gives a warning in a certain scenario.
> Reading this once per R session will be enough.
> i.e. it's not necessary to be shown in subsequent function calls.
>
> What's the best way to implement this?
> Some package-specific option?
> Where would I find good information on that?
>
> searching with the keywords I'm using as the subject of this message didn't dig up any results.
> Maybe it will be enough to give me some better keywords...
>
> Thanks ahead,
> Berry
>
> RclickHandbuch.wordpress.com
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list