在文档中显示knitr代码块源

我试图将一个针织大块的来源输出到投影仪幻灯片上。

例如,我想要在.Rnw中显示下面的代码块:

<<code-chunk, echo=TRUE, tidy=TRUE>>=
@

我试图重新使用以下行为:

<<out-first-code-chunk, echo=FALSE, comment=NA>>=
cat(paste("<<example-code-chunk, echo=TRUE, tidy=TRUE>>=","@",sep="n"))
@

这个代码是合法的,因为R的控制台中的cat命令给出了:

> cat('<<example-code-chunk, echo=TRUE, tidy=TRUE>>=','@',sep='n')
<<code-chunk, echo=TRUE, tidy=TRUE>>=
@

但是,所得到的乳胶:

begin{frame}
frametitle{Code Chunk}
To incorporate R code into your knitr documents
begin{knitrout}
definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}color{fgcolor}begin{kframe}
begin{verbatim}
<<example-code-chunk, echo=TRUE, tidy=TRUE>>=
@
end{verbatim}
end{kframe}
end{knitrout}

引发错误:

<<example-code-chunk, echo=TRUE, tidy=TRUE>>= @ end {verbatim} end
ETC. ! Paragraph ended before @xverbatim was complete. <to be read
again> par l.198 end{frame} I suspect you've forgotten a `}',
causing me to apply this control sequence to too much text. How can we
recover? My plan is to forget the whole thing and hope for the best. !
LaTeX Error: begin{verbatim} on input line 198 ended by
end{beamer@framepau ses}. See the LaTeX manual or LaTeX Companion for
explanation. Type H <return> for immediate help. ... l.198 end{frame}
Your command was ignored. Type I <command> <return> to replace it with
another command, or <return> to continue without it. ! LaTeX Error:
begin{kframe} on input line 198 ended by end{beamer@frameslide }.

为什么胶乳环境认为逐字未关闭? 是否有一种更恰当的方式来显示整个代码块?


这应该做到这一点...

设置块中有1行,输出块需要1个额外的参数...

安慰:

`install.packages(devtools)`  
`devtools::install_github("thell/knitliteral")`

对于.Rnw

<<"knitr-setup", include=FALSE, cache=FALSE>>=
knitLiteral::kast_on()
@

<<"my_chunk", eval=FALSE, opts.label="literal-literal">>=
# Something that will not be output in the doc.
@

输出:

<<"my_chunk", eval=FALSE>>=
@

对于.Rmd

````{r knitr_setup, include=FALSE, cache=FALSE}
knitLiteral::kast_on()
````

````{r my_chunk, opts.label="literal-literal"}
# Something that will not be output in the doc.
````

输出:

````{r my_chunk}
````

**使用4个反引号将语法高亮显示为有效的R (在哪里使用)。

从这个块,以及您可以在Literal Markdown文档示例的源代码中看到的内容以及不需要复杂块的渲染文档。

sweave示例文件也可以显示相同的示例。

链接地址: http://www.djcxy.com/p/79471.html

上一篇: Display the knitr code chunk source in document

下一篇: What is the most efficient code to detect and redirect SNI supported browsers?