Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pmcharrison/cache
cache - An R package for caching function outputs
https://github.com/pmcharrison/cache
Last synced: 8 days ago
JSON representation
cache - An R package for caching function outputs
- Host: GitHub
- URL: https://github.com/pmcharrison/cache
- Owner: pmcharrison
- License: other
- Created: 2018-11-20T19:02:24.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-28T08:34:27.000Z (about 6 years ago)
- Last Synced: 2024-08-13T07:15:06.152Z (4 months ago)
- Language: R
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - pmcharrison/cache - cache - An R package for caching function outputs (R)
README
---
output: github_document
---```{r setup, include = FALSE}
library(cache)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# cache - Caching function outputs[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/pmcharrison/cache?branch=master&svg=true)](https://ci.appveyor.com/project/pmcharrison/cache)
[![Travis build status](https://travis-ci.org/pmcharrison/cache.svg?branch=master)](https://travis-ci.org/pmcharrison/cache)
[![Coverage status](https://coveralls.io/repos/github/pmcharrison/cache/badge.svg)](https://coveralls.io/r/pmcharrison/cache?branch=master)
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)`cache` is an R package for function output caching, or memoisation.
The [memoise](https://cran.r-project.org/web/packages/memoise/index.html) package
is an effective tool for caching repeated computations in data analysis projects.
However, the memoise package cannot (easily) memoise
internal functions within R packages,
especially when the demand is for file-based memoisation,
or when memoisation options need to be exposed to the end user.This package addresses these problems.
It is designed for memoising functions when creating R packages,
while allowing subsequent package users to control cache options
(e.g. disabling cache, clearing the cache, changing the cache directory).## Installation
You can install the package directly from Github.
``` r
if (!require(devtools)) install.packages("devtools")
devtools::install_github("pmcharrison/cache")
```
## Example usageDefine a simple cached function:
```{r}
f <- function(x, ...) cache({
message("Computing...")
c(x, rnorm(1))
}, ...)
```The first time it's called, the main body of the function is run.
```{r}
f(1)
```The second time it's called, the result is loaded from the file-based cache.
```{r}
f(1)
```Our function `f` can be called with various caching options -
see `?cache::cache` for details.
These options include:- `.cache` - Whether or not to use cache on the current call.
- `.cache_memory` - Enables in-memory caching.
- `.cache_dir` - Directory in which to save cached files.
- `.cache_force` - If TRUE, an error will be thrown if the current call
cannot be found in the cache. Useful for debugging.If we call `f` with cache disabled, we should get a different result.
```{r}
f(1, .cache = FALSE) # produces a different result
```We can also define an in-memory cache.
In-memory caches are faster than the default file-based caches.```{r}
x <- cache_memory()
f(1, .cache_memory = x)f(1, .cache_memory = x)
as.list(x)[[1]]
```When we're done, we can clear the cache.
```{r}
clear_cache()
```