Print the Sourced R File to an Appendix Using Sweave

Print the sourced R file to an appendix using Sweave

How about using a Latex package?

Add into your header

\usepackage{fancyvrb}

Then

\VerbatimInput{yourRfile.R}

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

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:

screenshot

I think it's possible to modify the format a bit; see ?Rtangle for some details. Similar things are possible with knitr, but it's more flexible. I suspect the best method would be similar to the one you found for RMarkdown.

using source(X.R) in Sweave file

When you are writing code normally inside the code blocks of the document, just make sure to set the directory of where the R file is located and source the file.

e.g.,

<<echo=TRUE>>=
setwd(mydirectory)
source(myfile)
print(MEAN(x))
@

Sweave/R - Automatically generating an appendix that contains all the model summaries/plots/data profiles from an analysis

We made an attempt at this with our recent JASA article: http://hdl.handle.net/1902.1/12174. You should be able to "make" the whole paper. One thing to notice about our reproduction archive: we packaged versions of the R packages that we used. It turned out that as people improve their packages, sometimes they change defaults --- which would break our build. Perhaps in the future one might distribute an entire virtual machine including the R binary which would be called [recall how round(x,digits=) lost its arguments and became positional from version of R to the next -- making round(digits=,x) provide nonsense results without warning?].

Anyway, this is our first attempt at such a complex document. I have a smaller version here http://hdl.handle.net/1902.1/13376 which does not use make.

how to produce a sweave document without angle bracket in front of code chunks?

I think options(prompt = " ") at the top of your script will do it.

prompt (in options()) controls the text string used for the prompt in an interactive session and I'm assuming it will do the same for a document processed through Sweave.

EDIT: Thanks to Ben Bolker for pointing out that options(prompt = " ", continue = " ") will also take care of the "+" problem as well.

How do I Sweave a multiple-file project?

Forget for a second that you are dealing with Sweave and just think of the latex problem -- for which \include and \includeonly offer solutions. Try that with a few simple test files.

Once you have that figured out, fold Sweave back into the mix and it just work as Sweave is after 'merely' a pre-processing step, albeit a very clever one.

How to control the echo width using Sweave

You can use capture.output() to capture the printed representation of the list and then use writeLines() and strwrap() to display this output, nicely wrapped. As capture.output() returns a vector of strings containing the printed representation of the object, we can cat each of them to the screen/page but wrapped using strwrap(). The benefit of this approach is that the result looks like it was printed by R. Here's the solution:

writeLines(strwrap(capture.output(my_list)))

which produces:

$example
[1] "Site location was fixed using a Silvia Navigator
handheld GPS in October 2003. Point of reference used
was the station Bench Mark. If the bench mark location
was remote from the site then the point of reference used
was changed to the 0-1 metre gauge. Bench Mark location
was then recorded as a separate entry in the Site History
section [but not used as the site location].\r\nFor a
Station location map and all digital photograph's of the
station, river reach, and site details see
H:\\hyd\\dat\\doc. For non digital photo's taken prior
to October 2003 please see the relevant station file at
Tumut office."


Related Topics



Leave a reply



Submit