knitr 缓存:如果数据文件发生变化而不是其他选项(例如,`fig.xyz`)则更新
knitr cache: update if data file changes but not other options (e.g., `fig.xyz`)
假设我使用 knitr
,我有一个块需要一段时间才能 运行,我希望这个块在文件更改时更新,但如果我更改 [=13] =].后来建议我改cache
chunk option to 1 but then I cannot use a check sum as suggested here.
这是一个降价文件的例子
---
title: "Example"
author: "Benjamin Christoffersen"
date: "September 2, 2018"
output: html_document
---
```{r setup, include=FALSE}
data_file <- "~/data.RDS"
knitr::opts_chunk$set(echo = TRUE, cache.extra = tools::md5sum(data_file))
```
```{r load_data}
dat <- readRDS(data_file)
```
```{r large_computation, cache = 1}
Sys.sleep(10)
Sys.time() # just to that result do not change
```
```{r make_some_plot}
hist(dat)
```
运行 set.seed(1): saveRDS(rnorm(100), "~/data.RDS")
和针织产量
然后运行宁set.seed(2): saveRDS(rnorm(100), "~/data.RDS")
和针织产量
显示 large_computation
未按原样更新,因为 cache.extra
不在 knitr:::cache1.opts
向量中。当然,我可以保存 md5sum
结果,检查以前存储的文件并使用 cache.rebuild
或在 large_computation
块中做类似的事情,但如果使用 knitr
会更好解决方案。我经常发现我更改了一些块选项(例如 dpi
、fig.width
和 fig.height
),因此使用 cache = TRUE
将不起作用。我想可以修改包以便能够向 knitr:::cache1.opts
.
添加选项
如果我理解正确的话,问题是如果cache
设置为1
,则cache.extra
没有被考虑在内。事实上,this is by design.
期望的行为是在外部文件(或更一般的:提供给 cache.extra
的某些值)更改时使所有块(包括具有 cache = 1
的块)的缓存无效。
如问题中所述,实现此目的的一种方法是使用 chunk option cache.rebuild
但不是手动跟踪外部文件中的更改,我会利用 knitr 的内置缓存功能:
```{r cachecontrol, cache = TRUE, cache.extra = tools::md5sum(data_file)}
knitr::opts_chunk$set(cache.rebuild = TRUE)
```
将其添加为早期块,如果 data_file
更改,所有后续块的缓存将失效。这个想法是缓存控制后续块缓存的块——但前提是外部文件未更改。
当然,只有在评估 cachecontrol
块之前没有更改全局块选项时,这才有效。
问题的完整示例:
运行set.seed(1); saveRDS(rnorm(100), "data.RDS")
用不同的seed生成不同的外部文件,然后knit:
---
title: "Invalidate all chunks condidional on external file (even if cache=1)"
output: html_document
---
```{r}
data_file <- "data.RDS"
```
```{r cachecontrol, include = FALSE, cache = TRUE, cache.extra = tools::md5sum(data_file)}
# do NOT change global chunk options before this chunk
knitr::opts_chunk$set(cache.rebuild = TRUE)
```
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 8)
```
```{r load_data}
dat <- readRDS(data_file)
```
```{r large_computation, cache = 1}
Sys.sleep(10)
Sys.time() # just to show that result do not change unless external file changes
```
```{r make_some_plot}
hist(dat)
```
当缓存=1或2时,我找到了另一个解决cache.extra无知的方法。
请将以下 hook 代码插入设置部分,这会在代码部分插入额外的注释,以便在更改 cache.extra 时使缓存无效。
knitr::opts_hooks$set(cache.extra = function(options){
# invalidate cache
options$code <- c(sprintf("# cache.extra: %s", options$cache.extra), options$code)
options
})
假设我使用 knitr
,我有一个块需要一段时间才能 运行,我希望这个块在文件更改时更新,但如果我更改 [=13] =].后来建议我改cache
chunk option to 1 but then I cannot use a check sum as suggested here.
这是一个降价文件的例子
---
title: "Example"
author: "Benjamin Christoffersen"
date: "September 2, 2018"
output: html_document
---
```{r setup, include=FALSE}
data_file <- "~/data.RDS"
knitr::opts_chunk$set(echo = TRUE, cache.extra = tools::md5sum(data_file))
```
```{r load_data}
dat <- readRDS(data_file)
```
```{r large_computation, cache = 1}
Sys.sleep(10)
Sys.time() # just to that result do not change
```
```{r make_some_plot}
hist(dat)
```
运行 set.seed(1): saveRDS(rnorm(100), "~/data.RDS")
和针织产量
然后运行宁set.seed(2): saveRDS(rnorm(100), "~/data.RDS")
和针织产量
显示 large_computation
未按原样更新,因为 cache.extra
不在 knitr:::cache1.opts
向量中。当然,我可以保存 md5sum
结果,检查以前存储的文件并使用 cache.rebuild
或在 large_computation
块中做类似的事情,但如果使用 knitr
会更好解决方案。我经常发现我更改了一些块选项(例如 dpi
、fig.width
和 fig.height
),因此使用 cache = TRUE
将不起作用。我想可以修改包以便能够向 knitr:::cache1.opts
.
如果我理解正确的话,问题是如果cache
设置为1
,则cache.extra
没有被考虑在内。事实上,this is by design.
期望的行为是在外部文件(或更一般的:提供给 cache.extra
的某些值)更改时使所有块(包括具有 cache = 1
的块)的缓存无效。
如问题中所述,实现此目的的一种方法是使用 chunk option cache.rebuild
但不是手动跟踪外部文件中的更改,我会利用 knitr 的内置缓存功能:
```{r cachecontrol, cache = TRUE, cache.extra = tools::md5sum(data_file)}
knitr::opts_chunk$set(cache.rebuild = TRUE)
```
将其添加为早期块,如果 data_file
更改,所有后续块的缓存将失效。这个想法是缓存控制后续块缓存的块——但前提是外部文件未更改。
当然,只有在评估 cachecontrol
块之前没有更改全局块选项时,这才有效。
问题的完整示例:
运行set.seed(1); saveRDS(rnorm(100), "data.RDS")
用不同的seed生成不同的外部文件,然后knit:
---
title: "Invalidate all chunks condidional on external file (even if cache=1)"
output: html_document
---
```{r}
data_file <- "data.RDS"
```
```{r cachecontrol, include = FALSE, cache = TRUE, cache.extra = tools::md5sum(data_file)}
# do NOT change global chunk options before this chunk
knitr::opts_chunk$set(cache.rebuild = TRUE)
```
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 8)
```
```{r load_data}
dat <- readRDS(data_file)
```
```{r large_computation, cache = 1}
Sys.sleep(10)
Sys.time() # just to show that result do not change unless external file changes
```
```{r make_some_plot}
hist(dat)
```
当缓存=1或2时,我找到了另一个解决cache.extra无知的方法。 请将以下 hook 代码插入设置部分,这会在代码部分插入额外的注释,以便在更改 cache.extra 时使缓存无效。
knitr::opts_hooks$set(cache.extra = function(options){
# invalidate cache
options$code <- c(sprintf("# cache.extra: %s", options$cache.extra), options$code)
options
})