Questions tagged [memoise]

An R package to cache the results of a function, so when the function called with the same arguments it can retrieve pre-computed values.

27 questions
73
votes
3 answers

Options for caching / memoization / hashing in R

I am trying to find a simple way to use something like Perl's hash functions in R (essentially caching), as I intended to do both Perl-style hashing and write my own memoisation of calculations. However, others have beaten me to the punch and have…
Iterator
  • 20,250
  • 12
  • 75
  • 111
6
votes
1 answer

why are these memoised functions different?

I see that if I use memoise on a function in two different ways, I get two different behaviours, and I'd like to understand why. # Non Memoised function fib <- function(n) { if (n < 2) return(1) fib(n - 2) + fib(n -…
kmace
  • 1,994
  • 3
  • 23
  • 39
5
votes
2 answers

How to memoise a function at package startup in R

I work on an R package which wraps API calls. In order to reduce the number of actual calls and to speed things up, I memoise the function making the API call. To do so, I created the following function, which allows to set the cache…
der_grund
  • 1,898
  • 20
  • 36
3
votes
2 answers

rlang::hash cannot differentiate between arrow queries

I use the memoise package to cache queries to an arrow dataset but I sometimes get mismatches/"collisions" in hashes and therefore the wrong values are returned. I have isolated the problem and replicated it in the MWE below. The issue is that the…
David
  • 9,216
  • 4
  • 45
  • 78
2
votes
1 answer

Need to inspect memoised package function for memoisation to work

Let's say I have a package with the following function: foo <- function() { Sys.sleep(1) # really expensive operation return(1) } The value of the function is always the same per run, so I would like to use memoisation. I thought I could simply…
Wasabi
  • 2,879
  • 3
  • 26
  • 48
2
votes
0 answers

How to use Memoise in R

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…
2
votes
0 answers

R: How can I cache items in in memory in Shiny?

I am currently experiencing issues with caching items for use within Shiny (in R) and would like to know the correct way of doing this. An example would be that I need to load a large data.table using readRDS which is then sliced and diced various…
Mark
  • 43
  • 7
2
votes
1 answer

Cache read_html

I tried to cache read_html/xml2 to avoid flooding the server during development library(digest) library(xml2) url = "https://en.wikipedia.org" cache = digest(url) if (file.exists(cache)) { cat("Reading from cache\n") html = readRDS(cache) } else…
Dieter Menne
  • 10,076
  • 44
  • 67
1
vote
1 answer

Retrieve memoised Object using readRDS() and hash

Consider the following code: library(cachem) library(memoise) cache.dir <- "/Users/abcd/Desktop/temp_cache/" cache <- cachem::cache_disk(dir = cache.dir, max_size = 1024^2) fun <- function (x) {x^2} fun.memo <- memoise(f = fun, cache =…
Abdirizak
  • 80
  • 1
  • 7
1
vote
1 answer

Is this the correct way of writing memoise function in JavaScript?

Memoize Function in JavaScript. Is this the correct way of writing memoize function in JavaScript? Please provide correct one if it is wrong. My Code: let cache = {} function sum(a,b) { console.log(`Entering sum function ${a} & ${b}`) …
1
vote
0 answers

How to combine future_promise with memoise in R plumber

I am making an API with R package plumber. Some endpoints in this API would benefit from memoization, so I also use the memoise package. Combining both is pretty straight-forward, as demonstrated in this blog post. I'll build up on this post's…
Vongo
  • 1,325
  • 1
  • 17
  • 27
1
vote
1 answer

Amount of available memory in cache initialised with the R function: `memoise::memoise()`

I am repeatedly calling a memoised function initialised as so: library(memoise) memoised_func <- memoise::memoise(func) I am aware that I can set the size of the cache allocated to memoised_func with the cache argument…
Ben Jeffrey
  • 714
  • 9
  • 18
1
vote
0 answers

R memoise on Linux server

I'm testing memoise to cache a big dataframe in a R shiny application. It works totally fine on my local windows machine but as soon as I deploy the code on the Linux server, somehow it seems memoise doesn't do anything and every time I try to call…
Angelo
  • 1,594
  • 5
  • 17
  • 50
1
vote
1 answer

Load current cache using memoise

Let's assume I have run a memoised function that returns an integer output. Now, I do not know what inputs were used to store the current cache. Is there any way that I can get the current integer output that has been cached if using the memoise…
aajkaltak
  • 1,437
  • 4
  • 20
  • 28
1
vote
0 answers

Memoisation with RStudio's "Go to function"

I'm frequently use Rstudio's "Go To Function Definition" (shortcut is F2) in order to navigate between many files and quickly access a function's definition / make changes (printing the function's definition is usually not enough). In order to make…
1
2