With RMarkdown, I try to render a parametrized report for different values of a parameter. The Rmd file use caching.
The caching works as intended if I knit in RStudio, with the knit button : cache built at first, then used at each successive knitting, even if I change the parameter value in the YAML header.
But when looping with my parameters values and using rmarkdown::render() the cache is rebuilt at each iteration.
The test.Rmd file
---
title: "Untitled"
author: "Author"
params:
id: 0
date: "23/10/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Test `r params$id`
```{r cars, cache=TRUE}
## open and work on large file (simulate)
test <- mtcars
Sys.sleep(10)
```
And the rendering script : render.R
library(rmarkdown)
library(tidyverse)
1:5 %>%
walk(function(x) render("test.Rmd",
params = list(id = x),
output_file = paste0("file", x, ".html")))
The script takes 5 * 10 seconds to run instead of about 10 seconds.
What did I do wrong? How to use the cache?