Sweave FAQ

Friedrich Leisch

June 23, 2008

Contents

A Frequently Asked Questions
 A.1 Where can I find the manual and other information on Sweave?
 A.2 How can I get Emacs to automatically recognize files in Sweave format?
 A.3 Can I run Sweave directly from a shell?
 A.4 Why does LATEX not find my EPS and PDF graphic files when the filename contains a dot?
 A.5 Why does Sweave by default create both EPS and PDF graphic files?
 A.6 Empty figure chunks give LATEX errors.
 A.7 Why do R lattice graphics not work?
 A.8 How can I get Black & White lattice graphics?
 A.9 Creating several figures from one figure chunk does not work
 A.10 How can I place all those auto-generated graphics files in a subdirectory rather than the same directory as the Sweave file?
 A.11 How can I set default par() settings for figure chunks?
 A.12 Running latex fails on Windows
 A.13 How can I change the formatting of S input and output chunks?
 A.14 How can I change the line length of S input and output?
 A.15 Can I use Sweave for Word files?
 A.16 Can I use Sweave for OpenOffice files?
 A.17 Can I use Sweave for HTML files?
 A.18 After loading package R2HTML Sweave doesn’t work properly!
 A.19 Why does Sweave delete all comments from the R code? Why does it mess up line breaks for continuation lines?

A Frequently Asked Questions

A.1 Where can I find the manual and other information on Sweave?

The newest version of the Sweave manual can always be found at the Sweave homepage

http://www.stat.uni-muenchen.de/~leisch/Sweave

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).

A.2 How can I get Emacs to automatically recognize files in Sweave format?

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.

A.3 Can I run Sweave directly from a shell?

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.

A.4 Why does LATEX not find my EPS and PDF graphic files when the filename contains a dot?

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.

A.5 Why does Sweave by default create both EPS and PDF graphic files?

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.

A.6 Empty figure chunks give LATEX errors.

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.

A.7 Why do R lattice graphics not work?

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.

A.8 How can I get Black & White lattice 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  
@

A.9 Creating several figures from one figure chunk does not work

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="")  
}  
@

A.10 How can I place all those auto-generated graphics files in a subdirectory rather than the same directory as the Sweave file?

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.

A.11 How can I set default par() settings for figure chunks?

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.

A.12 Running latex fails on Windows

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:

  1. Install R in a path not containing any blanks.
  2. Copy the file Sweave.sty to a directory in your tex path or the directory containing the Sweave file and put a \usepackage{Sweave} into the preamble of your Sweave file.

A.13 How can I change the formatting of S input and output chunks?

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.

A.14 How can I change the line length of S input and output?

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).

A.15 Can I use Sweave for Word files?

No.

A.16 Can I use Sweave for OpenOffice files?

Yes, package odfWeave provides functions for using Sweave in combination with OpenOffice Writer rather than LATEX.

A.17 Can I use Sweave for HTML files?

Yes, package R2HTML provides a driver for using Sweave in combination with HTML rather than LATEX.

A.18 After loading package R2HTML Sweave doesn’t work properly!

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.

A.19 Why does Sweave delete all comments from the R code? Why does it mess up line breaks for continuation lines?

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}