Skip to content
Prev 343406 / 398506 Next

Display warning only once in session

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: