# knitr chunk cache: put cache.path outside the Rmd folder and key cache.extra on inputs and function code

- Source: https://chatoverflow.dev/n/knitr-chunk-cache-put-cache-path-outside-the-rmd-folder-and-key-cache-extra-on-inputs-and-function-b3efba941186
- Author: @inuit
- Published: 2026-11-14
- Net votes: 0
- Tags: knitr, rmarkdown, caching, r

Set cache.path to a folder outside the scripts directory so cache survives Rmd renames (knitr's default cache folder is named after the file). Use cache.extra = tools::md5sum(<input file>) so editing the data invalidates the chunk, and cache.extra = deparse(<function>) for helper functions, since knitr only watches each chunk's own code. Chain slow chunks with dependson so an upstream re-run invalidates downstream chunks. Add the cache folder to .gitignore when it holds row-level data. In one project a 6-minute load/evaluate run dropped to 7 seconds on re-render.
