Sweave User Manual
Friedrich Leisch and R-core
August 14, 2015
1 Introduction
Sweave provides a flexible framework for mixing text and R code for automatic document
generation. A single source file contains both documentation text and R code, which are then
woven into a final document containing
• the documentation text together with
• the R code and/or
• the output of the code (text, graphs)
This allows the re-generation of a report if the input data change and documents the code to
reproduce the analysis in the same file that contains the report. The R code of the complete
analysis is embedded into a L
A
T
E
X document
1
using the noweb syntax (Ramsey, 1998) which is
usually used for literate programming Knuth (1984). Hence, the full power of L
A
T
E
X (for high-
quality typesetting) and R (for data analysis) can be used simultaneously. See Leisch (2002)
and references therein for more general thoughts on dynamic report generation and pointers to
other systems.
Sweave uses a modular concept using different drivers for the actual translations. Obviously
different drivers are needed for different text markup languages (L
A
T
E
X, HTML, . . . ). Several
packages on CRAN provide support for other word processing systems (see Appendix A).
2 Noweb files
noweb (Ramsey, 1998) is a simple literate-programming tool which allows combining program
source code and the corresponding documentation into a single file. A noweb file is a simple
text file which consists of a sequence of code and documentation segments, called chunks:
Documentation chunks start with a line that has an at sign (@) as first character, followed
by a space or newline character. The rest of this line is a comment and ignored. Typically
documentation chunks will contain text in a markup language like L
A
T
E
X. The chunk
continues until a new code or documentation chunk is started: note that Sweave does not
interpret the contents of documentation chunks, and so will identify chunk indicators even
inside L
A
T
E
X verbatim environments.
Code chunks start with <<options>>= at the beginning of a line; again the rest of the line is
a comment and ignored.
The default for the first chunk is documentation.
In the simplest usage of noweb, the options (if present) of the code chunks give the names
of source code files, and the tool notangle can be used to extract the code chunks from the
1
http://www.ctan.org
1
评论1