The newest version of the Sweave manual can always be found at the Sweave homepage
where you also find several example files, and the lisp and shell code snippets of the FAQ. In addition, the homepage has several papers on Sweave like the CompStat paper and the 2-part miniseries from R News (Issues 2/3 and 2/3).
Recent versions of ESS (Emacs speaks statistics, http://ess.R-project.org) automatically recognize files with extension .Rnw as Sweave files and turn on the correct modes. Please follow the instructions on the ESS homepage on how to install ESS on your computer.
E.g., for writing makefiles it can be useful to run Sweave directly from a shell rather than manually start R and then run Sweave. This can easily be done using
R CMD Sweave file.Rnw
|
A more elaborate solution which also includes automatically running latex has been written by Gregor Gorjanc and is available at http://www.bfro.uni-lj.si/MR/ggorjan/software/shell/Sweave.sh.
Sweave uses the standard LATEX package graphicx to handle graphic files, which automatically uses EPS files for standard LATEX and PDF files for PDFLATEX, if the name of the input file has no extension, i.e., contains no dots. Hence, you may run into trouble with graphics handling if the name of your Sweave file contains extra dots: foo.Rnw is OK, while foo.bar.Rnw is not.
The LATEX package graphicx needs EPS files for plain LATEX, but PDF files for PDFLATEX (the latter can also handle PNG and JPEG files). Sweave automatically creates graphics in EPS and PDF format, such that the user can freely run latex or pdflatex on the final document as needed.
When a code chunk with fig=true does not call any plotting functions invalid EPS and PDF files are created. Sweave cannot know if the code in a figure chunk actually plotted something or not, so it will try to include the graphics, which is bound to fail.
The commands in package lattice have different behavior than the standard plot commands in the base package: lattice commands return an object of class "trellis", the actual plotting is performed by the print method for the class. Encapsulating calls to lattice functions in print() statements should do the trick, e.g.:
<<fig=TRUE>>=
library(lattice) print(bwplot(1:10)) @ |
should work. Future versions of Sweave may have more automated means to deal with trellis graphics.
What is the most elegant way to specify that strip panels are to have transparent backgrounds and graphs are to be in black and white when lattice is being used with Sweave? I would prefer a global option that stays in effect for multiple plots.
Answer by Deepayan Sarkar: I’d do something like this as part of the initialization:
<<...>>
library(lattice) ltheme <- canonical.theme(color = FALSE) ## in-built B&W theme ltheme$strip.background$col <- "transparent" ## change strip bg lattice.options(default.theme = ltheme) ## set as default @ |
Consider that you want to create several graphs in a loop similar to
<<fig=TRUE>>
for (i in 1:4) plot(rnorm(100)+i) @ |
This will currently not work, because Sweave allows only one graph per figure chunk. The simple reason is that Sweave opens a postscript device before executing the code and closes it afterwards. If you need to plot in a loop, you have to program it along the lines of
<<results=tex,echo=FALSE>>=
for(i in 1:4){ file=paste("myfile", i, ".eps", sep="") postscript(file=file, paper="special", width=6, height=6) plot(rnorm(100)+i) dev.off() cat("\\includegraphics{", file, "}\n\n", sep="") } @ |
After
\SweaveOpts{prefix.string=foo/bar}
|
Sweave will place all figures in subdirectory foo and their name will start with bar (instead of the name of the Sweave file). The subdirectory foo should exist before you run Sweave.
Because each EPS and PDF file opens a new device, using par() has only an effect if it is used inside a figure chunk. If you want to use the same settings for a series of figures, it is easier to use a hook function than repeating the same par() statement in each figure chunk.
The effect of
options(SweaveHooks=list(fig=function() par(bg="red", fg="blue")))
|
should be easy to spot. Do not forget to remove the hook at the end of the Sweave file unless you want to use it as a global option for all Sweave files.
If you can create the .tex file by running Sweave() in R, but cannot convert the .tex file to .dvi or .pdf, this is most likely caused by a space in the path of your R installation. If the path of your R installation contains any blank characters (like the default "c:\Program Files\..." in English versions of Windows), this may cause problems, because programs like tex or latex cannot handle blanks in paths properly.
Two possible solutions:
Sweave uses the fancyvrb package for formatting all S code and text output. fancyvrb is a very powerful and flexible package that allows fine control for layouting text in verbatim environments. If you want to change the default layout, simply read the fancyvrb documentation and modify the definitions of the Sinput and Soutput environments in Sweave.sty, respectively.
Sweave respects the usual way of specifying the desired line length in S, namely options(width). E.g., after options(width=40) lines will be formatted to have at most 40 characters (if possible).
No.
Yes, package odfWeave provides functions for using Sweave in combination with OpenOffice Writer rather than LATEX.
Yes, package R2HTML provides a driver for using Sweave in combination with HTML rather than LATEX.
Package R2HTML registers an Sweave driver for HTML files, and after that the Syntax for HTML is in the search list before the default syntax.
options(SweaveSyntax="SweaveSyntaxNoweb")
|
or calling Sweave like
Sweave(..., syntax="SweaveSyntaxNoweb")
|
ensures the default syntax even after loading R2HTML.
Sweave runs all code through the R parser. The “input lines” you see are the result from running the code through parse() and deparse(), which by default discards all comments and reformats line breaks. If you want to keep the code as it is in the source file, use
\SweaveOpts{keep.source=TRUE}
|