【问题标题】:Splitting knitr chunk code and its output拆分 knitr 块代码及其输出
【发布时间】:2014-08-15 11:45:33
【问题描述】:

这是this one 的后续问题。下面给出了我的带输出的 MWE。第二个 R 块代码没有任何输出。所以我不想把 knitrout 分成两部分。

代码

\documentclass{article} 
\begin{document}

<<setup, include=FALSE>>=
knit_hooks$set(
source = function(x, options) {
      x = knitr:::hilight_source(x, 'latex', options)
      if (options$highlight) {
        if (options$engine == 'R' || x[1] != '\\noindent') {
          paste(c('\\noindent\\textbf{R Code:}\\begin{alltt}', x, '\\end{alltt}', '','\\end{kframe} \\noindent and \\begin{kframe}\\noindent\\textbf{R Output:}'),
                collapse = '\n')
        } else {
          if ((n <- length(x)) > 5) x[n - 3] = sub('\\\\\\\\$', '', x[n - 3])
          paste(c('\\noindent\\textbf{R Code:}',x, '','\\noindent\\textbf{R Output:}'),
                collapse = '\n')
        }
      } else .verb.hook(x)
    }
)
@

Here's your first chunk.

<<chunk1, results = "hold" >>=
1:100
args(lm)
@ 

And here's another.

<<chunk2, results = "hold">>=
X <- 1:100
@ 

That seems to be it.

\end{document}

输出

【问题讨论】:

  • 设置results = 'hide'?
  • 感谢@rawr 的评论。由于某些原因,我需要评估在下一个块中使用 X。
  • 设置results = 'hide' 不能达到目的。仍然得到不需要的输出。
  • 如果我理解正确,您不希望第二个代码块出现在您的文档中,但您希望代码评估。在这种情况下设置echo = FALSE

标签: r latex knitr sweave


【解决方案1】:

你可以修改你的钩子,添加results = 'hide'。用你的钩子and \\begin{kframe}\\noindent\\textbf{R Output:}' 总是 printet。

\documentclass{article} 
\begin{document}

<<setup, include=FALSE>>=
knit_hooks$set(
source = function(x, options) {
      x = knitr:::hilight_source(x, 'latex', options)
      if (options$highlight) {
        if (options$engine == 'R' || x[1] != '\\noindent') {
          if(options$results == 'hide'){
             paste(c('\\noindent\\textbf{R Code:}\\begin{alltt}', x, '\\end{alltt}'),
                  collapse = '\n')
          } else {
            paste(c('\\noindent\\textbf{R Code:}\\begin{alltt}', x, '\\end{alltt}', '','\\end{kframe} \\noindent and \\begin{kframe}\\noindent\\textbf{R Output:}'),
                  collapse = '\n')
          }
        } else {
          if ((n <- length(x)) > 5) x[n - 3] = sub('\\\\\\\\$', '', x[n - 3])
          paste(c('\\noindent\\textbf{R Code:}',x, '','\\noindent\\textbf{R Output:}'),
                collapse = '\n')
        }
      } else .verb.hook(x)
    }
)
@

Here's your first chunk.

<<chunk1, results = "hold" >>=
1:100
args(lm)
@ 

And here's another.

<<chunk2, results = "hide">>=
X <- 1:100
@ 

That seems to be it.

\end{document}

【讨论】:

  • 您的解决方案工作正常,除了这些代码&lt;&lt;label=test22, results="hide"&gt;&gt;= ctl &lt;- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt &lt;- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group &lt;- gl(2, 10, 20, labels = c("Ctl","Trt")) weight &lt;- c(ctl, trt) lm.D9 &lt;- lm(weight ~ group) par(mfrow=c(2,2)) plot(lm.D9) par(mfrow=c(1,1))@ 任何想法。
  • 我不明白这里有什么问题。不打印“R 输出”。也许你想要results='hide', fig.show='hold'?如果没有,请更新您的问题和示例,以更明确地说明您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多