在knitr中缓存内联代码块

时间:2016-08-03 08:14:47

标签: r knitr r-markdown

有没有人知道是否以及如何在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 

1 个答案:

答案 0 :(得分:2)

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

只需设置该块以禁止代码,警告和输出的可见性,然后设置cache=TRUE。它基本上运行并隐藏结果。然后,您将数据保存到变量中。您可以随时通过将变量插入内联代码来调用它。并且它将在以下块中保持可用。

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