Memoise a function.

mf <- memoise(f) creates mf, a memoised copy of f. A memoised copy is basically a lazier version of the same function: it saves the answers of new invocations, and re-uses the answers of old ones. Under the right circumstances, this can provide a very nice speedup indeed.

memoise(f, ..., envir = environment(f), cache = cache_memory())
Function of which to create a memoised copy.
optional variables specified as formulas with no RHS to use as additional restrictions on caching. See Examples for usage.
Environment of the returned function.
Cache function.

There are two main ways to use the memoise function. Say that you wish to memoise glm, which is in the stats package; then you could use mem_glm <- memoise(glm), or you could use glm <- memoise(stats::glm). The first form has the advantage that you still have easy access to both the memoised and the original function. The latter is especially useful to bring the benefits of memoisation to an existing block of R code. Two example situations where memoise could be of use:

  • You're evaluating a function repeatedly over the rows (or larger chunks) of a dataset, and expect to regularly get the same input.
  • You're debugging or developing something, which involves a lot of re-running the code. If there are a few expensive calls in there, memoising them can make life a lot more pleasant. If the code is in a script file that you're source()ing, take care that you don't just put glm <- memoise(stats::glm) at the top of your file: that would reinitialise the memoised function every time the file was sourced. Wrap it in if (!is.memoised(glm)) , or do the memoisation call once at the R prompt, or put it somewhere else where it won't get repeated.

See Also

forget, is.memoised, timeout,

  • memoise
  • memoize
# a() is evaluated anew each time. memA() is only re-evaluated
# when you call it with a new set of parameters.
a <- function(n) { runif(n) }
memA <- memoise(a)
replicate(5, a(2))
replicate(5, memA(2))

# Caching is done based on parameters' value, so same-name-but-
# changed-value correctly produces two different outcomes...
N <- 4; memA(N)
N <- 5; memA(N)
# ... and same-value-but-different-name correctly produces
#     the same cached outcome.
N <- 4; memA(N)
N2 <- 4; memA(N2)

# memoise() knows about default parameters.
b <- function(n, dummy="a") { runif(n) }
memB <- memoise(b)
memB(2, dummy="a")
# This works, because the interface of the memoised function is the same as
# that of the original function.
# However, it doesn't know about parameter relevance.
# Different call means different caching, no matter
# that the outcome is the same.
memB(2, dummy="b")

# You can create multiple memoisations of the same function,
# and they'll be independent.
memA2 <- memoise(a)
memA(2)  # Still the same outcome
memA2(2) # Different cache, different outcome

# Don't do the same memoisation assignment twice: a brand-new
# memoised function also means a brand-new cache, and *that*
# you could as easily and more legibly achieve using forget().
# (If you're not sure whether you already memoised something,
#  use is.memoised() to check.)
memA <- memoise(a)
# Making a memoized automatically time out after 10 seconds.
memA3 <- memoise(a, ~{current <- as.numeric(Sys.time()); (current - current %% 10) %/% 10 })

# The timeout function is an easy way to do the above.
memA4 <- memoise(a, ~timeout(10))
Documentation reproduced from package memoise, version 1.1.0, License: MIT + file LICENSE

Community examples

Looks like there are no examples yet.