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.

Travis-CI Build Status Coverage Status


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 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


and you can test whether a function is memoised with

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



External Caches

memoise also supports external caching in addition to the default in-memory caches.

  • cache_filesystem() allows caching using files on a local filesystem. You can point this to a shared file such as dropbox or google drive to share caches between systems.
  • cache_s3() allows caching on Amazon S3


Use cache_s3() to cache objects using s3 storage. Requires you to specify a bucket using cache_name. When creating buckets, they must be unique among all s3 users when created.

Sys.setenv("AWS_ACCESS_KEY_ID" = "<access key>",
           "AWS_SECRET_ACCESS_KEY" = "<access secret>")
mrunif <- memoise(runif, cache = cache_s3("<unique bucket name>"))
mrunif(10) # First run, saves cache
mrunif(10) # Loads cache, results should be identical


cache_filesystem can be used for a file system cache. This is useful for preserving the cache between R sessions as well as sharing between systems when using a shared or synced files system such as Dropbox or Google Drive.

fc <- cache_filesystem("~/.cache")
mrunif <- memoise(runif, cache = fc)
mrunif(20) # Results stored in local file
dbc <- cache_filesystem("~/Dropbox/.rcache")
mrunif <- memoise(runif, cache = dbc)
mrunif(20) # Results stored in Dropbox .rcache folder which will be synced between computers.
gdc <- cache_filesystem("~/Google Drive/.rcache")
mrunif <- memoise(runif, cache = gdc)
mrunif(20) # Results stored in Google Drive .rcache folder which will be synced between computers.


Version 1.1.0

  • Caches now hash the function body along with the arguments, to ensure functions with identical arguments use a separate file-system cache. (#38)
  • Handle missing arguments in memoised functions for simple cases not using non-standard-evaluation (#19).
  • memoise() gains a cache= argument to specify an external cache. Two types of caches are available, cache_s3() for amazon S3 and cache_filesystem() for a file system cache (#25, @danielecook).

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)

New features

  • 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.


1.1.0 by Jim Hester, a year ago

Report a bug at

Browse source code at

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, TreeSearch, available, biolink, civis, corpustools, devtools, googleAnalyticsR, googleAuthR, heemod, kimisc, lidR, m2r, nat.templatebrains, nlmixr, opencage, prcbench, reproducible, rgho, saeRobust, toaster.

Depended on by GSED, SamplingStrata, gWidgets2RGtk2, gWidgets2tcltk.

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

See at CRAN