在 knitr 中缓存内联代码块

Cacheing inline code chunk in knitr

有谁知道是否可以以及如何在 knitr/rmarkdown 中缓存内联代码块?

这是我的出发点,但行不通:

---
title: "Test of inline chunk caching"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(cache = TRUE)
```

## Test of inline chunk caching

Test test `r Sys.sleep(10)` test test 

我不知道缓存内联代码的方法。但是您可以创建一段代码,其中包含您将在文本和内联代码中扩展的所有相同变量和计算。

只需设置该块以抑制代码、警告和输出的可见性,然后设置 cache=TRUE。它基本上运行并隐藏结果。然后你将数据保存到一个变量中。您可以随时通过将变量插入在线代码来调用它。它将在后续区块中保持可用。

This link takes you to a cheat sheet with all of the arguments for chunks on the second page.