You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -344,31 +344,30 @@ As expected, this only took (5 $\times$ 2 = ) 10 seconds to generate the new res
344
344
345
345
### Aside 1: Caching across R sessions
346
346
347
-
```{r pdf_cache1, echo = FALSE}
348
-
if (knitr::is_latex_output()){
349
-
message(
350
-
"Note: The benchmarked timing in this section might not line up with the text if you are viewing
351
-
the PDF version of the lecture notes."
352
-
)
353
-
}
354
-
```
355
-
356
-
357
347
The previous paragraph elides an important caveat: The default `memoise()` cache is only valid for the current R session. You can see this more clearly by exploring the help documentation of the function, where you will note the internal `cache = cache_memory()` argument. To enable caching that persists across sessions --- including when your computer crashes --- you need to specify a dedicated cache directory with `cache = cache_filesystem(PATH)`. This directory can be located anywhere on your system (or, indeed, on a linked cloud storage service) and you can even have multiple cache directories for different projects. My only modest recommendation is that you use a `.rcache/` naming pattern to keep things orderly.
358
348
359
349
For example, we can specify a new, persistent memoise cache location for our `slow_square()` function within this lecture sub-directory as follows.
Run our new memoised function and check that it saved the cached output to the specified directory.
370
369
371
-
```{r m4}
370
+
```{r m4, cache=FALSE}
372
371
m4 = map_df(1:7, mem_square_persistent)
373
372
374
373
cached_files = list.files(cache_dir)
@@ -379,21 +378,12 @@ cached_files
379
378
380
379
### Aside 2: Verbose output
381
380
382
-
```{r pdf_cache2, echo = FALSE}
383
-
if (knitr::is_latex_output()){
384
-
message(
385
-
"Note: The benchmarked timing in this section might not line up with the text if you are viewing
386
-
the PDF version of the lecture notes."
387
-
)
388
-
}
389
-
```
390
-
391
381
It's possible (and often very helpful) to add verbose prompts to our memoised functions. Consider the code below, which folds our `mem_square_persistent()` function into two sections:
392
382
393
383
1. Check for and load previously cached results. Print the results to screen.
394
384
2. Run our memoised function on any inputs that have not already been evaluated.( These results will be cached in turn for future use.) Again, print the results to screen.
And here's an example of the verbose function in action. The output is probably less impressive in a knitted R Markdown document, but I find the real-time feedback to be very informative in a live session. (Try it yourself.)
Finally, albeit probably unnecessary, we can also prove to ourselves that we've added the three new cases (i.e. for `8:10`) to our cache directory by comparing to what we had previously.
0 commit comments