Splitting the scribe Code and output into two different knitrouts

The knitr Chunk results = "hold" option may put the output after the Chunk Code . I wonder how to split the knitr Chunk code and output it into two different knitrouts , possibly with the heading Code and Output . Thanks in advance for your help.

 \documentclass{article} \begin{document} << label=Test, results = "hold" >>= 1:100 args(lm) @ \end{document} 

Desired Result


the code

 1:100 args(lm) 

and


Exit

  [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 [91] 91 92 93 94 95 96 97 98 99 100 function (formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...) 

Edited

I know that this can be done by putting two pieces showing only the code, and the other - only the code. For a long document, this is an extra hassle. I wonder if this can be obtained using some kind of hook.

+7
r sweave knitr latex
source share
2 answers

You will have to play with formatting, but you can achieve this by changing the source binding . What I'm showing below is actually a very simple modification of the main render_latex hook , which adds \\noindent\\textbf{Code:} before the code and \\noindent\\textbf{Output:} after this:

 \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{Code:}\\begin{alltt}', x, '\\end{alltt}', '','\\noindent\\textbf{Output:}'), collapse = '\n') } else { if ((n <- length(x)) > 5) x[n - 3] = sub('\\\\\\\\$', '', x[n - 3]) paste(c('\\noindent\\textbf{Code:}',x, '','\\noindent\\textbf{Output:}'), collapse = '\n') } } else .verb.hook(x) } ) @ Here your first chunk. <<chunk1, results = "hold" >>= 1:100 args(lm) @ And here another. <<chunk2, results = "hold">>= 1:5 6:10 @ That seems to be it. \end{document} 

Here is the result:

enter image description here

Thanks to the slight modification suggested by @mrbcuda in the comments, you can separate the code and the output frames:

Here's a modification to the setup fragment:

 <<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{Code:}\\begin{alltt}', x, '\\end{alltt}', '','\\end{kframe}\\begin{kframe}\\noindent\\textbf{Output:}'), collapse = '\n') } else { if ((n <- length(x)) > 5) x[n - 3] = sub('\\\\\\\\$', '', x[n - 3]) paste(c('\\noindent\\textbf{Code:}',x, '','\\noindent\\textbf{Output:}'), collapse = '\n') } } else .verb.hook(x) } ) @ 

And the resulting result:

enter image description here

+5
source share

I'm not sure exactly what you want to do, but does it give you the desired result? I divided the task into two pieces. Firstly, I postponed the evaluation of the first fragment and only printed the output in the second.

 \documentclass{article} \begin{document} \subsection{Code} <<label=chunk1, eval=FALSE>>= 1:10 args(lm) @ \subsection{Output} <<label=chunk2, echo=FALSE>>= <<chunk1>> @ \end{document} 
+5
source share

All Articles