Giter Site home page Giter Site logo

memoise's Introduction

memoise

Travis-CI Build Status Coverage Status CRAN_Status_Badge

Memoization

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

Installation

devtools::install_github("r-lib/memoise")

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
  • cache_gcs() allows caching on Google Cloud Storage
  • cache_mongo() allows caching on MongoDB GridFS

AWS 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

Filesystem

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.

Google Cloud Storage

cache_gcs saves the cache to Google Cloud Storage. It requires you to authenticate by downloading a JSON authentication file, and specifying a pre-made bucket:

library(googleCloudStorageR)
# Set GCS credentials.
Sys.setenv("GCS_AUTH_FILE"="<google-service-json>",
           "GCS_DEFAULT_BUCKET"="unique-bucket-name")

gcs <- cache_gcs()
mrunif <- memoise(runif, cache = gcs)
mrunif(10) # First run, saves cache
mrunif(10) # Loads cache, results should be identical

Mongo GridFS

cache_mongo() allows to store cache on a MongoDB back-end, using the GridFS API. You will first need to create a connection using mongolite::gridfs()

library(mongolite)
mongo_grid <- gridfs(
  db = "memoize",
  prefix = "memoize"
)

mong <- cache_mongo(mongo_grid)
mem_runif <- memoise(runif, cache = mong)

mrunif(10) # First run, saves cache
mrunif(10) # Loads cache, results should be identical

memoise's People

Contributors

ajdm avatar colinfay avatar coolbutuseless avatar csgillespie avatar dy-kim avatar egnha avatar hadley avatar jimhester avatar jsta avatar karldw avatar krlmlr avatar markedmondson1234 avatar mdsumner avatar mpadge avatar richardkunze avatar richierocks avatar sietse avatar stelsemeyer avatar wch avatar xhdong-umd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.