Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 分块代码及其输出_R_Latex_Knitr_Sweave - Fatal编程技术网

R 分块代码及其输出

R 分块代码及其输出,r,latex,knitr,sweave,R,Latex,Knitr,Sweave,这是我们的后续问题。我的MWE和输出如下所示。第二个R区块代码没有任何输出。所以我不想把针线鱼分成两块 代码 \documentclass{article} \开始{document} = 针织钩$套( 源=函数(x,选项){ x=knitr:::hilight\u源(x,'latex',选项) 如果(选项$highlight){ 如果(选项$engine=='R'| | x[1]!='\\noindent'){ 粘贴(c('\\noindent\\textbf{R代码:}\\begin{all

这是我们的后续问题。我的MWE和输出如下所示。第二个R区块代码没有任何输出。所以我不想把针线鱼分成两块

代码
\documentclass{article}
\开始{document}
=
针织钩$套(
源=函数(x,选项){
x=knitr:::hilight\u源(x,'latex',选项)
如果(选项$highlight){
如果(选项$engine=='R'| | x[1]!='\\noindent'){
粘贴(c('\\noindent\\textbf{R代码:}\\begin{alltt}',x'\\end{alltt}','''''''\\end{kframe}\\noindent和\\begin{kframe}\\noindent\\textbf{R输出:}),
折叠='\n')
}否则{
如果((n5)x[n-3]=sub('\\\$,'',x[n-3])
粘贴(c('\\noindent\\textbf{R代码:}',x','\\noindent\\textbf{R输出:}'),
折叠='\n')
}
}else.verb.hook(x)
}
)
@
这是你的第一块。
=
1:100
args(lm)
@ 
还有一个。
=

X您可以修改钩子,添加
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}
\documentclass{article}
\开始{document}
=
针织钩$套(
源=函数(x,选项){
x=knitr:::hilight\u源(x,'latex',选项)
如果(选项$highlight){
如果(选项$engine=='R'| | x[1]!='\\noindent'){
如果(选项$results=='hide'){
粘贴(c('\\noindent\\textbf{R代码:}\\begin{alltt}',x'\\end{alltt}'),
折叠='\n')
}否则{
粘贴(c('\\noindent\\textbf{R代码:}\\begin{alltt}',x'\\end{alltt}','''''''\\end{kframe}\\noindent和\\begin{kframe}\\noindent\\textbf{R输出:}),
折叠='\n')
}
}否则{
如果((n5)x[n-3]=sub('\\\$,'',x[n-3])
粘贴(c('\\noindent\\textbf{R代码:}',x','\\noindent\\textbf{R输出:}'),
折叠='\n')
}
}else.verb.hook(x)
}
)
@
这是你的第一块。
=
1:100
args(lm)
@ 
还有一个。
=

X set
results='hide'
?感谢@rawr的评论。我需要评估一些原因,以便在下一个块中使用X。设置
results='hide'
不起作用。仍然会得到不需要的输出。如果我理解正确,您不希望第二个代码块显示在文档中,但希望代码被删除aluate.Set
echo=FALSE
在这种情况下,您的解决方案可以正常工作,除了这些代码
=ctl我不知道这里有什么不起作用。“R输出”没有打印出来。可能您想要
results='hide',fig.show='hold'
?如果没有,请更新您的问题和示例,以便更明确地说明您的问题。
\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}