Search code examples
rshinyknitrr-markdownshiny-server

Possible to use knitr cache chunk in interactive rmarkdown doc?


I've noticed that when I have an Rmd with runtime: shiny in the YAML, code chunks don't seem to be read from cache. I'm wondering if using the shiny engine for rmarkdown just doesn't support chunk caching, or am I doing something wrong?

Example Rmd file:

---
title: "Cache test"
output: html_document
---

```{r cache=TRUE}
Sys.sleep(10)
```

If you run this 5 times, only the first time will take 10 seconds, and any subsequent run will be fast.

But if you add the runtime: shiny option to the YAML, then every single run will take 10 seconds.

(PS question: any better way to test whether or not code chunks cache is being used?)


Solution

  • i ran into the same problem where, in runtime: shiny, the cache switch was ignored.

    Nowadays there is a workaround, using runtime: shiny_prerendered and context="data" with cache=TRUE:

    ---
    title: "Cache test"
    output: html_document
    runtime: shiny_prerendered
    ---
    
    ```{r,context="data", cache=TRUE}
    Sys.sleep(10)
    ```
    

    this behaves as expected; on the first run, rendering takes 10 seconds; on all subsequent runs, the cached chunk is used.