Print the source R file in the application using Sweave

I save the R and Rnw files separately, then load the R data / graphs with load("file.R") into the first piece of Sweave. Is there a way that I can print the source R file in the application without executing all the code? (i.e. the code is slow enough so that I don't want its source() in the echo=TRUE block).

Thanks!


Update - in fact, I don't think my idea of source() works.

+2
r sweave
source share
4 answers

How about using the Latex package?
Add to your title
\ Usepackage {fancyvrb}
Then
\ VerbatimInput {yourRfile.R}

+5
source share

You can use the highlight package to output beautifully formatted, colorful code:

 highlight("myRfile.R", renderer = renderer_latex(document = F)) 

But remember to add the long preamble to your latex document that you get with the document = T.

You can experiment with the code directly:

 highlight(output="test.tex", parser.output = parser(text = deparse(lm)), renderer = renderer_latex(document = T)) 

And get

alt text

+3
source share

I usually solve this:

 \begin{appendix} \section{Appendix A} \subsection{R session information} <<SessionInforamtaion,echo=F,eval=T,results=tex>>= toLatex(sessionInfo()) @ \subsection{The simulation source code} <<SourceCode,echo=F,eval=T>>= Stangle(file.path("Projectpath","RnwFile.Rnw")) SourceCode <- readLines(file.path("Projectpath","Codefile.R")) writeLines(SourceCode) @ \end{appendix} 

Using this, you should think about the maximum number of characters per line.

+2
source share

Separation of R and Rnw files is a type of defeat for literate programming purposes. My own approach is to include code snippets in the appropriate place in the text. If my audience is not interested in the code, I can mark it as

 <<foo, echo=FALSE>>= x <- 1:10 @ 

I can compile the code in the application as

 <<appendix-foo, eval=FALSE>>= <<foo>> @ 

which I admit is a bit of shreds and a tendency to make mistakes (forgotten pieces). One quickly wants to associate a document with supporting material (data sets, useful supporting functions, non-R-scripts) in an R-package, and they are not difficult to create. Building the package automatically creates the pdf and Stangle'd R file that you want. Creating a package can be a slow process, but installing the package does not require the vignettes to be rebuilt, and therefore quickly and conveniently for those to whom you give the package.

To rotate with formatting / text, I use the global option \SweaveOpts{eval=FALSE} .

+2
source share

All Articles