9.2 Cache a code chunk for multiple output formats

When caching is turned on via the chunk option cache = TRUE, knitr will write R objects generated in a code chunk to a cache database, so they can be reloaded the next time. The path to the cache database is determined by the chunk option cache.path. By default, R Markdown uses different cache paths for different output formats, which means a time-consuming code chunk will be fully executed for each output format. This may be inconvenient, but there is a reason for this default behavior: the output of a code chunk can be dependent on the specific output format. For example, when you generate a plot, the output for the plot could be Markdown code like ![text](path/to/image.png) when the output format is word_document, or HTML code like <img src="path/to/image.png" /> when the output format is html_document.

When a code chunk does not have any side-effects (such as plots), it is safe to use the same cache database for all output formats, which can save you time. For example, when you read a large data object or run a time-consuming model, the result does not depend on the output format, so you can use the same cache database. You can specify the path to the database via the chunk option cache.path on a code chunk, e.g.,

```{r important-computing, cache=TRUE, cache.path="cache/"}
```

By default, cache.path = "INPUT_cache/FORMAT/" in R Markdown, where INPUT is the input filename, and FORMAT is the output format name (e.g., html, latex, or docx).

We do not recommend that you set cache = TRUE or cache.path globally in a document. Caching can be quite tricky. Instead, we recommend that you set these options only on individual code chunks that are surely time-consuming and do not have side-effects.