![](/img/trans.png)
[英]Splitting knitr Chunk code and output into two different knitrouts
[英]Splitting knitr chunk code and its output
這是后續問題這一個。 我的 MWE 輸出如下。 第二個 R 塊代碼沒有任何輸出。 所以我不想把 knitrout 分成兩塊。
\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}
您可以修改鈎子,添加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}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.