2

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 past year and then performs some calculations.

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. E.g. Files read for 1/12/19 would be between 1/12/18 to 1/12/19, files read for 2/12/2019 would be between 2/12/2018 and 2/12/2019. Essentially, there will be a lot of overlap in the files that b() reads. so it would essentially be ideal to memoise 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 painful 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 I think I did not implement it the right way as it threw an error saying 'could not modify fread'
  3. Would memoising fread in the global environment help? Again, I was not able to implement it the right way
  4. is there something drastically long in the way i am thinking about these functions and memoisation?

Any input would be appreciated.

0 Answers0