拆分knitr Chunk代码并输出到两个不同的knitrouts

MYa*_*208 7 latex r sweave knitr

knitr Chunk选项results = "hold"可以将输出放在后面Chunk Code.我想知道如何将knitr块代码和输出分成两个不同的knitrouts可能与标题CodeOutput.在此先感谢您的帮助.

\documentclass{article} 
\begin{document}

<< label=Test, results = "hold" >>=
1:100
args(lm)
@ 
\end{document}
Run Code Online (Sandbox Code Playgroud)

期望的输出


1:100
args(lm)
Run Code Online (Sandbox Code Playgroud)


产量

 [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, ...) 
Run Code Online (Sandbox Code Playgroud)

编辑

我知道这可以通过将两个块放在一个只显示代码而另一个只显示代码来完成.对于长文档,这是一个额外的麻烦.我想知道这是否可以通过一些钩子获得.

And*_*rau 5

我不确定你想要做什么,但这会给你想要的输出吗?我将任务分成两个块.首先,我推迟了第一个块的评估,只在第二个块中打印输出.

\documentclass{article}
\begin{document}

\subsection{Code}
<<label=chunk1, eval=FALSE>>=
1:10
args(lm)
@

\subsection{Output}
<<label=chunk2, echo=FALSE>>=
<<chunk1>>
@

\end{document}
Run Code Online (Sandbox Code Playgroud)

  • 您可以将第二个块表示为"<< chunk2,ref.label ="chunk1",echo = FALSE >> = @` (5认同)

Tho*_*mas 5

您必须使用格式化,但您可以通过修改source 代码钩子来实现此目的.我在下面展示的实际上是对代码之前和之后添加的基本render_latex钩子的一个非常简单的修改:\\noindent\\textbf{Code:}\\noindent\\textbf{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') {
          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's your first chunk.

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

And here's another.

<<chunk2, results = "hold">>=
1:5
6:10
@ 

That seems to be it.

\end{document}
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述

感谢@mrbcuda在评论中建议稍作修改意味着你可以分开代码和输出框架:

这是setup块的修改:

<<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)
    }
)
@
Run Code Online (Sandbox Code Playgroud)

结果输出:

在此输入图像描述