Caching chunks in RStudio does not work

I am trying to use knitr's cache feature in a rmd file created with RStudio like this:

```{r, cache=TRUE}
Sys.sleep(2)
a <- 1
```

I found this option by clicking on "Chunk options" and the ending up here: https://yihui.name/knitr/options/?version=1.1.453&mode=desktop

However, it seems that nothing is cached: The code evaluation always takes long and I cannot find a cache directory.

How can I get caching working in RStudio?

I've run your example (minus the last \), and I'm getting a cache. You'll need to provide some more details for us to look over. Try running a document with just this chunk:

```{r, cache=TRUE}
knitr::opts_current$get(c(
  "cache",
  "cache.path",
  "cache.rebuild",
  "dependson",
  "autodep"
))
```

You should see something like this:

## $cache
## [1] 3
## 
## $cache.path
## [1] "test_cache/html/"
## 
## $cache.rebuild
## [1] FALSE
## 
## $dependson
## NULL
## 
## $autodep
## [1] FALSE

If the cached objects have a large combined size in memory, it may be just taking that long to load them from disk. Here are two chunks to test if a cache is actually being created:

library(survival)
"survival" %in% loadedNamespaces()

The first time you knit the document, before there's a cache, the second chunk will give the result TRUE. Afterwards, if there is a cache, it'll show FALSE. This is because knitr doesn't capture loaded packages in cache.

1 Like

Thanks @nwerth. I corrected the question for the last \.
The output of

```{r, cache=TRUE}
knitr::opts_current$get(c(
  "cache",
  "cache.path",
  "cache.rebuild",
  "dependson",
  "autodep"
))
```

is not as expected:

$cache
NULL

$cache.path
NULL

$cache.rebuild
NULL

$dependson
NULL

$autodep
NULL

The test you proposed always gives TRUE, indicating, that cache does not work.

I'm in the same boat as @fabianrost.

Are you knitting or just running the chunk?

I am getting the same results as you (NULL cache).

As far as I know there are two incompatible caches when you work with Rmd and especially notebooks. One is a knitr cache which is only used when you knit the whole document. The knitr::opts_current obviously refers to that cache. The second cache is what RStudio itself uses when you run code snippets one by one. If you "preview" a document the results also come from that internal RStudio cache which has nothing to do with knitr package. knitr cannot access that cache.

This has been an constant source of confusion for our users who are surprised that even after they've evaluated all the snippets in a notebook with cache=TRUE running knit takes a long time because knit does not use those results. As far as I know there is no plans to reconcile the caches.

The short answer to your question is running knitr::opts_current$get in a snippet does not create knitr cache regardless of the value of the cache option.

I think archivist package (https://arxiv.org/pdf/1706.08822.pdf, https://pbiecek.github.io/archivist/index.html) suggests a workaroud to help avoid multiple evaluation (by providing yet another cache :grinning:). See this document for an example: https://rawgit.com/pbiecek/archivist/master/scripts/listOfPackages.html. In particular they provide addHooksToPrint function which stores and caches whatever you print. That way the cached results will be used in both snippet-by-snippet evaluation and while knitting. The downside is you bypass both RStudio and knitr caches.

2 Likes

I noticed:

  • Pushing Knit for vignette generates cache dir and files, but pushing knit again does not re-use cache.

  • Pushing Build > More > Configure Build Tools > Configure > ticking "Vignettes". does generate and re-use cache.

2 Likes