Show code in app with knitr

I have an Rnw file (large) and I want to show all the code used in the application.

Some examples (e.g. https://github.com/yihui/knitr-examples/blob/master/073-code-appendix.Rnw , also a good MWE) suggest the presence of this code:

<<Rcode, eval=FALSE, ref.label=all_labels()[-1],echo=TRUE, cache=FALSE>>= @ 

This works great, except that all the pieces of code merge with each other, and none of them are flagged.

On the other hand, if I run purl (myfile.Rnw), it calls the pieces of code and splits them into two lines, which greatly simplifies reading.

Is there a way to automatically list the code using the second approach in the report application? I know that I can have a piece of code to run purl to create myfile.R as part of my report, but how can I then show the code in myfile.R in my application?

+6
source share
1 answer

Here is an example .Rnw file (called "example.rnw"):

 \documentclass{article} \begin{document} <<a>>= x <- 1:10 x @ <<b>>= y <- 10:1 y @ <<c>>= z <- 1:5 z @ \clearpage \input{example-purl.tex} \end{document} 

If you create a file in your working directory called "template.rnw" that contains only:

 <<%sCHUNK_LABEL_HERE, eval=FALSE>>= @ 

Then you run:

 stitch(purl("example.rnw",output="example-purl.r"),template="template.rnw") knit("example.rnw") 

It makes sense? Basically, you purl ing, stitch with the purl ed, knit ting code of the source document, and then compile the resulting LaTeX ("example.tex"), which includes knit ting and purl ING. Everything should be formatted beautifully (and sequentially).

+3
source

Source: https://habr.com/ru/post/952256/


All Articles