-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
134 lines (98 loc) · 3.92 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
mustashe::clear_stash()
```
# mustashe <a href="https://jhrcook.github.io/mustashe/index.html"> <img src="man/figures/logo.png" align="right" alt="" width="120" /> </a>
<!-- badges: start -->
[](https://CRAN.R-project.org/package=mustashe)
[](https://github.com/jhrcook/mustashe/actions)
[](https://travis-ci.org/jhrcook/mustashe)
[](https://ci.appveyor.com/project/jhrcook/mustashe)
[](https://codecov.io/gh/jhrcook/mustashe?branch=master)
<!-- badges: end -->
The goal of 'mustashe' is to save time on long-running computations by storing and reloading the resulting object after the first run.
The next time the computation is run, instead of evaluating the code, the stashed object is loaded.
'mustashe' is great for storing intermediate objects in an analysis.
## Installation
You can install the released version of 'mustashe' from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("mustashe")
```
And the development version from [GitHub](https://github.com/jhrcook/mustashe) with:
``` r
# install.packages("devtools")
devtools::install_github("jhrcook/mustashe")
```
## Loading 'mustashe'
The 'mustashe' package is loaded like any other, using the `library()` function.
```{r example}
library(mustashe)
```
## Basic example
Below is a simple example of how to use the `stash()` function from 'mustashe'.
Let's say, for part of an analysis, we are running a long simulation to generate random data `rnd_vals`.
This is mocked below using the `Sys.sleep()` function.
We can time this process using the 'tictoc' library.
```{r}
tictoc::tic("random simulation")
stash("rnd_vals", {
Sys.sleep(3)
rnd_vals <- rnorm(1e5)
})
tictoc::toc()
```
Now, if we come back tomorrow and continue working on the same analysis, the second time this process is run the code is not evaluated because the code passed to `stash()` has not changed.
Instead, the random values `rnd_vals` is loaded.
```{r}
tictoc::tic("random simulation")
stash("rnd_vals", {
Sys.sleep(3)
rnd_vals <- rnorm(1e5)
})
tictoc::toc()
```
## Dependencies
A common problem with storing intermediates is that they have dependencies that can change.
If a dependency changes, then we want the stashed value to be updated.
This is accomplished by passing the names of the dependencies to the `depends_on` argument.
For instance, let's say we are calculating some value `foo` using `x`.
(For the following example, I will use a print statement to indicate when the code is evaluated.)
```{r}
x <- 100
stash("foo", depends_on = "x", {
print("Calculating `foo` using `x`.")
foo <- x + 1
})
foo
```
Now if `x` is not changed, then the code for `foo` does not get re-evaluated.
```{r}
x <- 100
stash("foo", depends_on = "x", {
print("Calculating `foo` using `x`.")
foo <- x + 1
})
foo
```
But if `x` does change, then `foo` gets re-evaluated.
```{r}
x <- 200
stash("foo", depends_on = "x", {
print("Calculating `foo` using `x`.")
foo <- x + 1
})
foo
```
---
### Attribution
The inspiration for this package came from the `cache()` feature in the ['ProjectTemplate'](http://projecttemplate.net/index.html) package.
While the functionality and implementation are a bit different, this would have been far more difficult to do without referencing the source code from 'ProjectTemplate'.