Display warning only once in session
Is local preferred to prevent *any* access to the internal environment of the returned function?
You can gain access to the environment of the function by using environment(myFunc), with either local() or your suggestion of making a factory function and calling it.
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))
}
}
That function, when called, would return a function that warned just once; it is not a function that warns just once. You could call it myFuncFactory and make myFunc itself with myFunc <- myFuncFactory(). That would be equivalent to my example calling local(). The factory approach is good for making a series of functions, each with its own state (say counters or random number generators). local() is convenient for singleton functions-with-state. You can also supply an environment to local() so you can make a set of functions sharing a common environment. I think reference classes encapsulate both of these cases. Bill Dunlap TIBCO Software wdunlap tibco.com
On Mon, Aug 25, 2014 at 5:15 PM, Grant Rettke <gcr at wisdomandwonder.com> wrote:
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.