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?)
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With