问题
Motivated by https://gist.github.com/yihui/2629886#file-knitr-checkpoint-rnw, I want to create a knitr hook that can overwrite certain options of a knitr chunk:
\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}
Globally, all code chunks are displayed in the output. The display of the output of the first chunk is disabled through the chunk option echo=FALSE
. This should be reset by the hook 'checkpoint'. However, this is not the case - most probably because the chunk options are evaluated before the hook gets called.
Is there another solution to this?
来源:https://stackoverflow.com/questions/24687728/knitr-overwrite-chunk-options-with-hook-function