Re: [R] Persistent state in a function?

2016-03-25 Thread Greg Snow
Here is the rough equivalent to what you did but using the R6 package: library(R6) Cache <- R6Class("Cache", public = list( myCache = numeric(0), add = function(x) { self$myCache <- c(self$myCache, x) print(self$myCache) } ) ) cacheThis <- Cache$new()

Re: [R] Persistent state in a function?

2016-03-25 Thread Boris Steipe
To follow on, for the record, here's the code example using local() instead: Comments appreciated. # === CODE cacheThis <- local({ # creates a closure, assigned to "cacheThis" myCache <- numeric()# a persistent variable in the closure's environment useCache <-

Re: [R] Persistent state in a function?

2016-03-23 Thread Boris Steipe
All - Thanks, this has been a real eye-opener. Here's my variation based on what I've learned so far. It's based on Bert's earlier function-returning-a-closure example. I hope I got the terminology right. # makeCache <- function(){ #

Re: [R] Persistent state in a function?

2016-03-23 Thread Martin Morgan
Use a local environment to as a place to store state. Update with <<- and resolve symbol references through lexical scope E.g., persist <- local({ last <- NULL# initialize function(value) { if (!missing(value)) last <<- value

Re: [R] Persistent state in a function?

2016-03-23 Thread Greg Snow
Boris, You may want to look into the R6 package. This package has tools that help create objects (environments) with methods that can use and change the object. You can have your persistent table stored as part of your object and then create methods that will use and modify the table within the

Re: [R] Persistent state in a function?

2016-03-21 Thread Bert Gunter
Yes, Duncan. My statement was wrong. I should have said that it's the environment/evaluation frame of f = g(). Thank you for the correction. Still, I hope my example was helpful. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking

Re: [R] Persistent state in a function?

2016-03-21 Thread Duncan Murdoch
On 21/03/2016 11:19 AM, Bert Gunter wrote: Martin, All: A very nice point! Perhaps the following may help to illustrate it. g <- function(){ x <- NULL function(y){cat("result is ",x," \n"); x <<- y} } > f <- g() > rm(g) # g is deleted but its environment remains as the environment of

Re: [R] Persistent state in a function?

2016-03-21 Thread Bert Gunter
Martin, All: A very nice point! Perhaps the following may help to illustrate it. g <- function(){ x <- NULL function(y){cat("result is ",x," \n"); x <<- y} } > f <- g() > rm(g) # g is deleted but its environment remains as the environment of f > f(1) result is > f(3) result is 1 >

Re: [R] Persistent state in a function?

2016-03-21 Thread Martin Maechler
> Duncan Murdoch > on Sat, 19 Mar 2016 17:57:56 -0400 writes: > On 19/03/2016 12:45 PM, Boris Steipe wrote: >> Dear all - >> >> I need to have a function maintain a persistent lookup table of results for an expensive calculation, a named

Re: [R] Persistent state in a function?

2016-03-19 Thread Duncan Murdoch
On 19/03/2016 12:45 PM, Boris Steipe wrote: Dear all - I need to have a function maintain a persistent lookup table of results for an expensive calculation, a named vector or hash. I know that I can just keep the table in the global environment. One problem with this approach is that the

Re: [R] Persistent state in a function?

2016-03-19 Thread Erich Neuwirth
package memoise might help you > On 19 Mar 2016, at 17:45, Boris Steipe wrote: > > Dear all - > > I need to have a function maintain a persistent lookup table of results for > an expensive calculation, a named vector or hash. I know that I can just keep > the table

Re: [R] Persistent state in a function?

2016-03-19 Thread Bert Gunter
Use an environment to hold your table. ?new.env or ?local (I leave it to you to work out details) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

[R] Persistent state in a function?

2016-03-19 Thread Boris Steipe
Dear all - I need to have a function maintain a persistent lookup table of results for an expensive calculation, a named vector or hash. I know that I can just keep the table in the global environment. One problem with this approach is that the function should be able to delete/recalculate the