拆分knitr Chunk代码和输出

时间:2014-08-15 11:45:33

标签: r latex knitr sweave

这是one的后续问题。我的MWE输出如下。第二个R Chunk code没有任何输出。所以我不想将针织物分成两部分。在这方面的任何帮助将受到高度赞赏。感谢


代码


\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}

输出


enter image description here

1 个答案:

答案 0 :(得分:1)

你可以修改你的钩子,添加results = 'hide'。钩子and \\begin{kframe}\\noindent\\textbf{R Output:}'始终是打印机。

\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}