knitr: Overwrite chunk options with hook function

断了今生、忘了曾经 提交于 2019-12-11 08:48:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!