只有 运行 knitr 对已更改的 Rnw 文件部分

Only run knitr on parts of Rnw file that have changed

假设我在 Rnw 文件中包含两个代码块,code_block_1code_block_2。假设我对 code_block_1 进行了更改,但 code_block_2 保持不变。

我正在使用 knitrRnw 文件转换为 tex 文件。因为code_block_2一直没变,我能不能让knitr只求值,运行code_block_1

首先在此处查看 knitr 的选项:http://yihui.name/knitr/options/。我认为您正在寻找的是 cache 选项。试试这个小例子,注意时间从一个 运行 另一个改变仅针对您实际更改代码的块:

第一个运行:

\documentclass{article}
\begin{document}

<<code_block_1, cache=TRUE>>=
set.seed(123)
x <- rnorm(10)
summary(x)
Sys.time()
@

<<code_block_2, cache=TRUE>>=
set.seed(123)
y <- rnorm(10)
summary(y)
Sys.time()
@

\end{document}

输出:

第二个运行(在第二个块中添加评论之后):

\documentclass{article}
\begin{document}

<<code_block_1, cache=TRUE>>=
set.seed(123)
x <- rnorm(10)
summary(x)
Sys.time()
@

<<code_block_2, cache=TRUE>>=
# Just added a comment in this chunk
set.seed(123)
y <- rnorm(10)
summary(y)
Sys.time()
@

\end{document}

输出: