由https://gist.github.com/yihui/2629886#file-knitr-checkpoint-rnw激励,我想创建一个可以覆盖knitr chunk的某些选项的knitr钩子:
\documentclass{article}
\begin{document}
<<setup>>=
knit_hooks$set(checkpoint = function(before, options, envir) {
if (!before) {
opts_chunk$set(echo = TRUE, include = TRUE)
opts_current$set(echo = TRUE, include = TRUE) ## no effect
}
})
opts_chunk$set(echo = TRUE, include = TRUE, checkpoint = "foobar")
@
<<example-a, echo=FALSE>>=
x = 1
@
<<example-b, echo=TRUE>>=
x = 2
@
\end{document}
在全局范围内,所有代码块都显示在输出中。通过块选项echo=FALSE
禁用第一个块的输出显示。这应该通过钩子'检查点'重置。但是,情况并非如此 - 很可能是因为在调用钩子之前会评估块选项。
还有其他解决办法吗?