Memoisation of Functions

Cache the results of a function so that when you call it again with the same arguments it returns the pre-computed value.


If a function is called multiple times with the same input, you can often speed things up by keeping a cache of known answers that it can retrieve. This is called memoisation http://en.wikipedia.org/wiki/Memoization. The memoise package provides a simple syntax

mf <- memoise(f)

to create mf(), a memoised wrapper around f(). You can clear mf's cache with

forget(mf)

, and you can test whether a function is memoised with

is.memoised(mf) # TRUE
is.memoised(f)  # FALSE

.

News

Version 1.0.0

  • memoise() now signals an error if an already memoised function is used as input (#4, @richierocks).
  • has_cache() function added which returns a boolean depending on if the given call is cached or not (#10, @dkesh).
  • Memoised functions now have a print method which displays the original function definition, rather than the memoisation code (#15, @jimhester).
  • A memoised function now has the same interface as the original function, if the original function is known when memoise is called. (Otherwise, the old behavior is invoked, with a warning.) (#14, @krlmlr)
  • The enclosing environment of the memoised function is specified explicitly, defaults to parent.frame().
  • is.memoised now checks if the argument is a function.
  • Testing infrastructure, full test coverage.

Version 0.2.1

  • Update to fix outstanding R CMD check issues.

Version 0.2 (2010-11-11)

  • Memoised functions now have an attribute memoised=TRUE, and is.memoised() tests whether a function is memoised. (Contributed by Sietse Brouwer.)
  • Documentation is now more elaborate, and hopefully more accessible to newcomers. Thanks to Sietse Brouwer for the verbosity.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("memoise")

1.1.0 by Jim Hester, 7 months ago


https://github.com/hadley/memoise


Report a bug at https://github.com/hadley/memoise/issues


Browse source code at https://github.com/cran/memoise


Authors: Hadley Wickham [aut], Jim Hester [aut, cre], Kirill Müller [aut], Daniel Cook [aut]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports digest

Suggests testthat, aws.s3, httr, covr


Imported by BWStest, DeLorean, LCAvarsel, OpenML, RSQLite, RxODE, SIDES, available, biolink, civis, corpustools, devtools, googleAnalyticsR, googleAuthR, heemod, lidR, m2r, nat.templatebrains, nlmixr, opencage, prcbench, rgho, saeRobust, toaster.

Depended on by GSED, SamplingStrata, gWidgets2RGtk2, gWidgets2tcltk.

Suggested by covr, functools, icd9, leaflet.esri, leaflet.extras, surveillance.


See at CRAN