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()
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 <-
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(){ #
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
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
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
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
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
>
> 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
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
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
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 )
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
13 matches
Mail list logo