简体   繁体   English

R Sweave:将.Rnw文件的整个代码放在附录中?

[英]R Sweave: put the whole code of the .Rnw file in Appendix?

I just found this awesome technique to put the code used in the.Rmd file in the appendix (of that same file).我刚刚发现了这种很棒的技术,可以将 .Rmd 文件中使用的代码放在(同一个文件的)附录中。

However, I am using R Sweave and not R Markdown and I would like to know if there exists a similar way to put all the code at the end in a unique chunk.但是,我使用的是 R Sweave 而不是 R Markdown,我想知道是否存在类似的方法将所有代码放在最后。 The code to do that in Markdown does not work in Sweave.在 Markdown 中执行此操作的代码在 Sweave 中不起作用。 I precise that, unlike this post , I do not have a separate.R file where the calculations are made.我确切地说,与这篇文章不同,我没有单独的.R 文件来进行计算。 Everything is done in the.Rnw file.一切都在 .Rnw 文件中完成。

Does anybody know how to do it?有人知道怎么做吗?

Edit : a reproducible example编辑:一个可重现的例子

\documentclass[11pt, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<reg2, echo=FALSE, print=FALSE>>=
head(mtcars)
@

<<reg3, echo=FALSE, print=FALSE>>=
head(iris)
@

\section*{Appendix}
% the place where I could like to put the whole code

\end{document}

This chunk works to include the code:此块用于包含代码:

<<echo=FALSE, eval=TRUE>>=
filename <- tempfile(fileext=".R")
Stangle("test.Rnw", output = filename, quiet = TRUE)
cat(readLines(filename), sep = "\n")
@

When I include that in your example file, I see this:当我将它包含在您的示例文件中时,我看到:

截屏

I think it's possible to modify the format a bit;我认为可以稍微修改格式; see ?Rtangle for some details.有关详细信息,请参阅?Rtangle Similar things are possible with knitr , but it's more flexible. knitr也可以做类似的事情,但它更灵活。 I suspect the best method would be similar to the one you found for RMarkdown.我怀疑最好的方法与您为 RMarkdown 找到的方法相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM