拆分knitr Chunk代码和输出

时间:2021-09-30 06:14:14

This is the followup question of this one. My MWE with output is given below. The second R Chunk code doesn't have any output. So I want not to split the knitrout into two pieces. Any help in this regard will be highly appreciated. Thanks

这是这个问题的后续问题。我的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}

Output


拆分knitr Chunk代码和输出

1 个解决方案

#1


1  

You could modify your hook, adding results = 'hide'. With your hook and \\begin{kframe}\\noindent\\textbf{R Output:}' is always printet.

你可以修改你的钩子,添加results ='hide'。使用你的钩子和\\ 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}

#1


1  

You could modify your hook, adding results = 'hide'. With your hook and \\begin{kframe}\\noindent\\textbf{R Output:}' is always printet.

你可以修改你的钩子,添加results ='hide'。使用你的钩子和\\ 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}

相关文章