[R] Display warning only once in session

Grant Rettke gcr at wisdomandwonder.com
Tue Aug 26 02:15:25 CEST 2014


Is local preferred to prevent *any* access to the internal environment
of the returned function?

How is it different than this?

myFunc <- function() {
    notWarnedYet <- TRUE
    function(x) {
        if (notWarnedYet) {
            warning("myFunc is funky")
            notWarnedYet <<- FALSE # note use of <<-
        }
        sqrt(log2(x))
    }
}
Grant Rettke | ACM, ASA, FSF, IEEE, SIAM
gcr at wisdomandwonder.com | http://www.wisdomandwonder.com/
“Wisdom begins in wonder.” --Socrates
((λ (x) (x x)) (λ (x) (x x)))
“Life has become immeasurably better since I have been forced to stop
taking it seriously.” --Thompson


On Mon, Aug 25, 2014 at 4:50 PM, William Dunlap <wdunlap at tibco.com> wrote:
> 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.
>
> ______________________________________________
> 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