Caching chunks in RStudio does not work

rmarkdown

#1

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?


#2

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.


#3

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.