I have a function of the below form. I vectorized it using purrr::map rather than using vectorize following this advise.
load_data <- function(key){
load_data_one <- function(key){
...
# somehow retrieve data based on key
...
data
}
# create result allowing key to be a vector
result <- map_dfr(key, load_data_one)
}
The routine to retrieve data is quite expensive. Having discovered the memoise package, I want to figure out the best way to use it in this situation. Ideally, I want to put it around the nested function load_data_one, so I imagine that when I call load_data for a vector, than only the unknown results would be calculated. If I memoise load_data instead, and I pass one vector only slightly different to another one that I passed earlier, would the memoise function be smart enough to only calculate the new elements? Also, are there any other issues related to applying memoise to the nested function load_data_one, that would require me to maybe put it outside rather than within load_data?
I suggest you define and memoise the
load_data_onefunction externally:This way, the memoization is done per individual
key, so if you start withload_data(c(1,3))and then callload_data(c(1,2,3)), the second call will only calculate for2, recalling the memoized results for1and3.