我有一个Rnw文件(一个大文件),我想显示附录中使用的所有代码.
在一些knitr示例(https://github.com/yihui/knitr-examples/blob/master/073-code-appendix.Rnw,也是一个优秀的MWE)中建议使用这样的代码块是办法:
<<Rcode, eval=FALSE, ref.label=all_labels()[-1],echo=TRUE, cache=FALSE>>=
@
Run Code Online (Sandbox Code Playgroud)
这工作正常,除了所有代码块相互合并且没有标记.
另一方面,如果我运行purl(myfile.Rnw),它会标记代码块并将它们分隔两行,这样可以更容易阅读.
有没有办法在报告附录中使用第二种方法自动列出代码?我知道我可以有一个代码块来运行purl来生成myfile.R作为我报告的一部分,但是如何在我的附录中显示myfile.R中的代码?
这是一个示例 .Rnw 文件(称为“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}
Run Code Online (Sandbox Code Playgroud)
如果您在工作目录中创建一个名为“template.rnw”的文件,其中仅包含:
<<%sCHUNK_LABEL_HERE, eval=FALSE>>=
@
Run Code Online (Sandbox Code Playgroud)
然后,你运行:
stitch(purl("example.rnw",output="example-purl.r"),template="template.rnw")
knit("example.rnw")
Run Code Online (Sandbox Code Playgroud)
那有意义吗?基本上,您正在purling、stitching purled 代码、knitting 原始文档,然后编译包含knitting 和purling 的结果 LaTeX(“example.tex”)。所有内容的格式都应该很好(并且一致)。
| 归档时间: |
|
| 查看次数: |
1749 次 |
| 最近记录: |