110 likes | 384 Views
Sweave. Adele Cutler. Sweave. Friedrich Leisch Department of Statistics Ludwig-Maximilians-Universität München http://www.statistik.lmu.de/~leisch/. What is Sweave?. Sweave is a framework for mixing high quality typesetting (LaTeX) code (R) graphs/tables/output
E N D
Sweave Adele Cutler
Sweave Friedrich Leisch Department of Statistics Ludwig-Maximilians-Universität München http://www.statistik.lmu.de/~leisch/
What is Sweave? Sweave is a framework for mixing • high quality typesetting (LaTeX) • code (R) • graphs/tables/output so that the report can be automatically re-created when the results or methodology change.
How Sweave Works: The author writes an Sweave file, an ordinary text file ending in SNW, containing • chunks of R • chunks of LaTeX • bits of glue that control everything Sweave creates a LaTeX file with the R code replaced, if desired, by its output. Stangle creates a file with just the R code (ready to be run in R).
Example Create an Sweave file called “small.SNW” and go into R. The R command Sweave(“small.SNW”) produces a new file called “small.SNW.tex”. We can now run LaTex in the usual way. The R command Stangle(“small.SNW”) produces a new file called “small.SNW.R”. We could execute these commands using source(“small.SNW.R”) from inside R.
Sweave Small.SNW Sweave() Stangle() Small.SNW.tex Small.SNW.R LaTeX with R results R commands only
Writing Sweave Files (_Snw) • LaTeX chunks start with @ and are just standard LaTeX. • To include scalars in text, use \Sexpr{expr}, where expr is an R expression returning a vector, the first element of which will be printed in the LaTeX.
Writing Sweave Files (continued) • R chunks start with <<name>>= • Global options can be added using \SweaveOpts{opt1=val1,…,optn=valn} anywhere in a LaTeX chunk. • Options can be added to a single R chunk using <<name,opt1=val1,…,optn=valn>>=
Useful Options echo=FALSE prevents the R code from showing up in the LaTeX file. fig=TRUE creates EPS and PDF files corresponding to the plots and inserts the relevant command into the LaTeX file. results=hide eval=false
Named R chunks • R chunks can be named and reused: <<a>>= x=10 @ <<b>>= x+y @ <<c>>= <<a>> y=20 <<b>> @ Is equivalent to <<c>>= x=10 y=20 x+y @
For More Information: Read the FAQ: http://www.statistik.lmu.de/~leisch/Sweave/FAQ.html Read the Manual: http://www.statistik.lmu.de/~leisch/Sweave/Sweave-manual.pdf