【发布时间】:2015-06-14 23:14:21
【问题描述】:
假设我在 Rnw 文件中包含两个代码块,code_block_1 和 code_block_2。假设我对code_block_1 进行了更改,但code_block_2 保持不变。
我正在使用knitr 将Rnw 文件转换为tex 文件。因为code_block_2 保持不变,我可以让knitr 只评估和运行code_block_1 吗?
【问题讨论】:
假设我在 Rnw 文件中包含两个代码块,code_block_1 和 code_block_2。假设我对code_block_1 进行了更改,但code_block_2 保持不变。
我正在使用knitr 将Rnw 文件转换为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}
输出:
【讨论】: