Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

How to use Memoise in R

$
0
0

I have been trying to use memoise to speed up computations for a function I wrote.

In a nutshell, I have a function a() that performs do.call() on another function b(). function b() runs code to read files over the last year.

So if you wanted to run a() for 1/12/2019, 2/12/2019, 3/12/2019, b() is going to read files for one year prior to 1/12, 2/12, 3/12. Essentially, there will be a lot of overlap in the files that b() reads. so it would essentially be ideal to memoise say, an fread function that b() uses.

I have written a sample function below

b<-function(){

##ideally would want to memoise it this way

if(!is.memoised(fread))

##fread=memoise(fread)

fread(...)
}
a<-function(){
do.call(b())
}

So I have discovered after many hours that this does not work in theory as the memoisation function has a constraint relating to how many times it can be called - i.e. if it is called more than once then it does not remember the previous function parameters that were used by the previously memoised fread, essentially eliminating the usefulness of the function)

  1. I suspect it has something to do with the scope of the memoised function, everytime b() is called it is memoising fread again
  2. I tried to implement <<- for memoising fread, but it did not pan out (in order to enforce scoping)
  3. Would memoising fread in the global environment help?
  4. is there something drastically long in the way i am thinking about these functions and memoisation?

Any input would be appreciated.


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>