Display the knitr code chunk source in document
I'm trying to output the source of a knitr chunk onto a beamer slide.
For example, I would like the following code chunk to be displayed as is in the .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:
> 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?
This should do it...
1 line in the setup chunk, and 1 extra param in the chunk desired for output...
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).
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.
The sweave example file is also available showing the same examples.
链接地址: http://www.djcxy.com/p/79472.html上一篇: 一次消费多条消息
下一篇: 在文档中显示knitr代码块源