memoise

Easy memoisation for R

https://github.com/r-lib/memoise

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    2 of 27 committers (7.4%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.4%) to scientific vocabulary

Keywords

memoise r

Keywords from Contributors

strings grammar data-manipulation visualisation unit-testing tidy-data setup string-interpolation s3-vectors devtools
Last synced: 10 months ago · JSON representation

Repository

Easy memoisation for R

Basic Info
Statistics
  • Stars: 320
  • Watchers: 9
  • Forks: 58
  • Open Issues: 44
  • Releases: 5
Topics
memoise r
Created over 15 years ago · Last pushed over 2 years ago
Metadata Files
Readme License

README.Rmd

---
output: github_document
editor_options:
  chunk_output_type: console
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

# memoise


[![CRAN status](https://www.r-pkg.org/badges/version/memoise)](https://CRAN.R-project.org/package=memoise)
[![R build status](https://github.com/r-lib/memoise/workflows/R-CMD-check/badge.svg)](https://github.com/r-lib/memoise/actions)
[![Codecov test coverage](https://codecov.io/gh/r-lib/memoise/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/memoise?branch=main)


The memoise package makes it easy to memoise R functions. **Memoisation** () caches function calls so that if a previously seen set of inputs is seen, it can return the previously computed output.

# Installation

Install from CRAN with

```{r, eval = FALSE}
install.packages("memoise")
```

## Usage

To memoise a function, use `memoise()`:

```{r}
library(memoise)
f <- function(x) {
  Sys.sleep(1)
  mean(x)
}
mf <- memoise(f)
```

```{r eval=FALSE}
system.time(mf(1:10))
#>    user  system elapsed
#>   0.002   0.000   1.003
system.time(mf(1:10))
#>    user  system elapsed
#>   0.000   0.000   0.001
```

You can clear `mf`'s cache with:

```{r eval=FALSE}
forget(mf)
```

And you can test whether a function is memoised with `is.memoised()`.

## Caches

By default, memoise uses an in-memory cache, using `cache_mem()` from the [cachem](https://cachem.r-lib.org/) package. `cachem::cache_disk()` allows caching using files on a local filesystem.

Both `cachem::cache_mem()` and `cachem::cache_disk()` support automatic pruning by default; this means that they will not keep growing past a certain size, and eventually older items will be removed from the cache. The default size `cache_mem()` is 512 MB, and the default size for a `cache_disk()` is 1 GB, but this can be customized by specifying `max_size`:

```{r}
# 100 MB limit
cm <- cachem::cache_mem(max_size = 100 * 1024^2)

mf <- memoise(f, cache = cm)
```

You can also change the maximum age of items in the cache with `max_age`:

```{r}
# Expire items in cache after 15 minutes
cm <- cachem::cache_mem(max_age = 15 * 60)

mf <- memoise(f, cache = cm)
```

By default, a `cache_disk()` uses a subdirectory the R process's temp directory, but it is possible to specify the directory. This is useful for persisting a cache across R sessions, sharing a cache among different processes, or even for synchronizing across the network.

```{r, eval = FALSE}
# Store in "R-myapp" directory inside of user-level cache directory
cd <- cachem::cache_disk(rappdirs::user_cache_dir("R-myapp"))

# Store in Dropbox
cdb <- cachem::cache_disk("~/Dropbox/.rcache")
```

A single cache object can be shared among multiple memoised functions. By default, the cache key includes not only the arguments to the function, but also the body of the function. This essentially eliminates the possibility of a cache collision, even if two memoised functions are called with the same arguments.

```{r}
m <- cachem::cache_mem()

times2 <- memoise(function(x) { x * 2 }, cache = m)
times4 <- memoise(function(x) { x * 4 }, cache = m)

times2(10)
times4(10)
```

### Cache API

It is possible to use other caching backends with memoise. These caching objects must be key-value stores which use the same API as those from the [cachem](https://cachem.r-lib.org/) package. The following methods are required for full compatibiltiy with memoise:

* `$set(key, value)`: Sets a `key` to `value` in the cache.
* `$get(key)`: Gets the value associated with `key`. If the key is not in the cache, this returns an object with class `"key_missing"`.
* `$exists(key)`: Checks for the existence of `key` in the cache.
* `$remove(key)`: Removes the value for `key` from the cache.
* `$reset()`: Resets the cache, clearing all key/value pairs.

Note that the sentinel value for missing keys can be created by calling `cachem::key_missing()`, or `structure(list(), class = "key_missing")`.


### Old-style cache objects

Before version 2.0, memoise used different caching objects, which did not have automatic pruning and had a slightly different API. These caching objects can still be used, but we recommend using the caching objects from cachem when possible.

With the old-style caching objects, memoise first checks for the existence of a key in the cache, and if present, it fetches the value. This results in a possible race condition (when using caches other than the memory cache): an object could be deleted from the cache after the existence check, but before the value is fetched. With the new cachem-style caching objects, the possibility of a a race condition is eliminated: memoise simply tries to fetch the key, and if it's not present in the cache, the cache returns a sentinel value indicating that it's missing. (Note that the caching objects must also be designed to avoid a similar race condition internally.)

The following cache objects do not currently have an equivalent in cachem.

-   `cache_s3()` allows caching on [Amazon S3](https://aws.amazon.com/s3/) Requires you to specify a bucket using `cache_name`. When creating buckets, they must be unique among all s3 users when created.

    ```{r, eval = FALSE}
    Sys.setenv(
      "AWS_ACCESS_KEY_ID" = "",
      "AWS_SECRET_ACCESS_KEY" = ""
    )
    cache <- cache_s3("")
    ```

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

    ```{r, eval = FALSE}
    Sys.setenv(
      "GCS_AUTH_FILE" = "",
      "GCS_DEFAULT_BUCKET" = "unique-bucket-name"
    )
    gcs <- cache_gcs()
    ```

Owner

  • Name: R infrastructure
  • Login: r-lib
  • Kind: organization

GitHub Events

Total
  • Issues event: 3
  • Watch event: 5
  • Issue comment event: 2
  • Fork event: 2
Last Year
  • Issues event: 3
  • Watch event: 5
  • Issue comment event: 2
  • Fork event: 2

Committers

Last synced: 12 months ago

All Time
  • Total Commits: 210
  • Total Committers: 27
  • Avg Commits per committer: 7.778
  • Development Distribution Score (DDS): 0.648
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Jim Hester j****r@g****m 74
Kirill Müller k****r@i****h 65
hadley h****m@g****m 19
Winston Chang w****n@s****g 14
Sietse Brouwer s****r@g****m 8
Eugene Ha e****a@p****e 3
Mark Edmondson g****b@m****e 3
Maximilian Girlich m****h@m****m 3
Karl Dunkle Werner k****w 2
mikefc c****s 2
dkesh d****h@q****m 1
Colin Gillespie c****e@g****m 1
Daeyoung Kim d****2@g****m 1
Daniel Possenriede p****e@g****m 1
Joseph Stachelek j****a 1
Kirill Müller k****r 1
Lionel Henry l****y@g****m 1
Mara Averick m****k@g****m 1
Michael Sumner m****r@g****m 1
Richard Cotton r****s@g****m 1
Salim B s****m@p****e 1
Tracy Teal t****l@g****m 1
ajdm a****m 1
mark padgham m****m@e****m 1
richardkunze 4****e 1
stelsemeyer 1****r 1
xianghui dong x****g@u****u 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 77
  • Total pull requests: 33
  • Average time to close issues: 6 months
  • Average time to close pull requests: 4 months
  • Total issue authors: 61
  • Total pull request authors: 24
  • Average comments per issue: 2.22
  • Average comments per pull request: 1.7
  • Merged pull requests: 17
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 2
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • DarwinAwardWinner (4)
  • krlmlr (3)
  • bluaze (3)
  • masato-yoshihara (2)
  • wlandau-lilly (2)
  • wurli (2)
  • dracodoc (2)
  • DavZim (2)
  • hadley (2)
  • mgirlich (2)
  • dmi3kno (2)
  • PeterNSteinmetz (1)
  • cboettig (1)
  • MalteThodberg (1)
  • hongooi73 (1)
Pull Request Authors
  • bluaze (4)
  • mattyb (3)
  • wch (3)
  • mgirlich (2)
  • coolbutuseless (2)
  • lionel- (1)
  • dmurdoch (1)
  • DavZim (1)
  • dpprdan (1)
  • cpsievert (1)
  • ColinFay (1)
  • richardkunze (1)
  • mdsumner (1)
  • stelsemeyer (1)
  • mbertolacci (1)
Top Labels
Issue Labels
feature (13) bug (6) documentation (2) help wanted :heart: (1)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • cran 736,736 last-month
  • Total docker downloads: 125,786,235
  • Total dependent packages: 152
    (may contain duplicates)
  • Total dependent repositories: 433
    (may contain duplicates)
  • Total versions: 10
  • Total maintainers: 1
cran.r-project.org: memoise

'Memoisation' of Functions

  • Versions: 6
  • Dependent Packages: 152
  • Dependent Repositories: 433
  • Downloads: 736,736 Last month
  • Docker Downloads: 125,786,235
Rankings
Downloads: 0.3%
Dependent packages count: 0.7%
Dependent repos count: 0.7%
Forks count: 1.2%
Stargazers count: 1.3%
Average: 3.6%
Docker downloads count: 17.3%
Maintainers (1)
Last synced: 11 months ago
proxy.golang.org: github.com/r-lib/memoise
  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 11 months ago

Dependencies

DESCRIPTION cran
  • cachem * imports
  • rlang >= 0.4.10 imports
  • aws.s3 * suggests
  • covr * suggests
  • digest * suggests
  • googleAuthR * suggests
  • googleCloudStorageR * suggests
  • httr * suggests
  • testthat * suggests
.github/workflows/R-CMD-check.yaml actions
  • actions/cache v1 composite
  • actions/checkout v2 composite
  • actions/upload-artifact main composite
  • r-lib/actions/setup-pandoc master composite
  • r-lib/actions/setup-r master composite
.github/workflows/pkgdown.yaml actions
  • actions/cache v1 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc master composite
  • r-lib/actions/setup-r master composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v2 composite
  • r-lib/actions/pr-fetch master composite
  • r-lib/actions/pr-push master composite
  • r-lib/actions/setup-r master composite
.github/workflows/test-coverage.yaml actions
  • actions/cache v1 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc master composite
  • r-lib/actions/setup-r master composite