在文档中显示knitr代码块源

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

I'm trying to output the source of a knitr chunk onto a beamer slide.

我正在尝试将knitr chunk的源输出到beamer幻灯片上。

For example, I would like the following code chunk to be displayed as is in the .Rnw:

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

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

I've attempted to recreate this behavior using:

我试图使用以下方法重新创建此行为:

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

This code is legitimate since the cat command in R's console gives:

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

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

However, the resulting latex:

但是,由此产生的乳胶:

\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}

Throws errors:

<<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 }.

Why is the latex environment thinking that verbatim was not closed? Is there a more appropriate way to display a code-chunk in its entirety?

为什么乳胶环境认为逐字不关闭?有没有更合适的方式来完整地显示代码块?

1 个解决方案

#1


4  

This should do it...

这应该做到......

1 line in the setup chunk, and 1 extra param in the chunk desired for output...

设置块中的1行,以及输出所需的块中的1个额外参数...

Console:

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

For .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.
@

Output:

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

For .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.
````

Output:

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

** The use of 4 backticks keeps syntax highlighting as valid R (where used).

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

From this chunk and what you can see in the source of the example Literal Markdown doc and the rendered doc that there is no need to have a complex chunk.

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

The sweave example file is also available showing the same examples.

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

#1


4  

This should do it...

这应该做到......

1 line in the setup chunk, and 1 extra param in the chunk desired for output...

设置块中的1行,以及输出所需的块中的1个额外参数...

Console:

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

For .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.
@

Output:

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

For .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.
````

Output:

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

** The use of 4 backticks keeps syntax highlighting as valid R (where used).

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

From this chunk and what you can see in the source of the example Literal Markdown doc and the rendered doc that there is no need to have a complex chunk.

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

The sweave example file is also available showing the same examples.

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