Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 300
DeanAttali Avatar asked May 14 '15 20:05

DeanAttali


People also ask

How do I use cache in R Markdown?

Caching a code chunk in R Markdown R Markdown has a built-in caching feature that can be enabled by setting cache=TRUE in the chunk's header. The second time the chunk is run, both the visual output and any objects created are loaded from disk.

How do you add a chunk in R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

How do I clean my knitr cache?

If you run into problems with cached output you can always clear the knitr cache by removing the folder named with a _cache suffix within your document's directory.


1 Answers

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.

like image 153
Janna Maas Avatar answered Sep 18 '22 06:09

Janna Maas