Sweave provides a flexible framework for mixing text and R/S code
for automatic report generation. The basic idea is to replace the
code with its output, such that the final document only contains the
text and the output of the statistical analysis: however, the source
code can also be included.
Path to Sweave source file. Note that this can be
supplied without the extension, but the function will only proceed
if there is exactly one Sweave file in the directory whose
basename matches file.
driver
The actual workhorse, see ‘Details’.
syntax
NULL or an object of class SweaveSyntax or
a character string with its name.
See the section ‘Syntax Definition’.
encoding
The default encoding to assume for file.
...
Further arguments passed to the driver's setup function:
see section ‘Drivers’, RweaveLatex and
Rtangle.
Details
Automatic generation of reports by mixing word processing markup (like
latex) and S code. The S code gets replaced by its output (text or
graphs) in the final markup file. This allows a report to be re-generated
if the input data change and documents the code to reproduce the
analysis in the same file that also produces the report.
Sweave combines the documentation and code chunks together (or
their output) into a single document. Stangle extracts only
the code from the Sweave file creating an S source file that can be
run using source. (Code inside Sexpr{}
statements is ignored by Stangle.)
Stangle is just a wrapper to Sweave specifying a
different default driver. Alternative drivers can be used: the CRAN
package cacheSweave and the Bioconductor package weaver
both provide drivers based on the default driver
RweaveLatex which incorporate ideas of caching
the results of computations on code chunks.
Environment variable SWEAVE_OPTIONS can be used to override the
initial options set by the driver: it should be a comma-separated set
of key=value items, as would be used in a SweaveOpts
statement in a document.
Non-ASCII source files must contain a line of the form
usepackage[foo]{inputenc}
(where foo is typically latin1, latin2, utf8 or
cp1252 or cp1250) or they will give an error.
Re-encoding can be turned off completely with argument encoding
= "bytes".
Syntax Definition
Sweave allows a flexible syntax framework for marking
documentation and text chunks. The default is a noweb-style syntax, as
alternative a latex-style syntax can be used. (See the user manual for
further details.)
If syntax = NULL (the default) then the available syntax
objects are consulted in turn, and selected if their extension
component matches (as a regexp) the file name. Objects
SweaveSyntaxNoweb (with extension = "[.][rsRS]nw$") and
SweaveSyntaxLatex (with extension = "[.][rsRS]tex$") are
supplied, but users or packages can supply others with names matching
the pattern SweaveSyntax.*.
Author(s)
Friedrich Leisch and R-core.
References
Friedrich Leisch (2002)
Dynamic generation of statistical reports using literate data analysis.
In W. Härdle and B. Rönz, editors,
Compstat 2002 - Proceedings in Computational Statistics,
pages 575–580. Physika Verlag, Heidelberg, Germany, ISBN 3-7908-1517-9.
Non-Sweave vignettes may be built with tools::buildVignette.
Examples
testfile <- system.file("Sweave", "Sweave-test-1.Rnw", package = "utils")
## enforce par(ask = FALSE)
options(device.ask.default = FALSE)
## create a LaTeX file
Sweave(testfile)
## This can be compiled to PDF by
## tools::texi2pdf("Sweave-test-1.tex")
## or outside R by
## R CMD texi2pdf Sweave-test-1.tex
## which sets the appropriate TEXINPUTS path.
## create an R source file from the code chunks
Stangle(testfile)
## which can be sourced, e.g.
source("Sweave-test-1.R")
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(utils)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/utils/Sweave.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Sweave
> ### Title: Automatic Generation of Reports
> ### Aliases: Sweave Stangle SweaveSyntaxLatex SweaveSyntaxNoweb
> ### Keywords: utilities
>
> ### ** Examples
>
> testfile <- system.file("Sweave", "Sweave-test-1.Rnw", package = "utils")
>
> ## enforce par(ask = FALSE)
> options(device.ask.default = FALSE)
>
> ## create a LaTeX file
> Sweave(testfile)
Writing to file Sweave-test-1.tex
Processing code chunks with options ...
1 : keep.source print term verbatim (Sweave-test-1.Rnw:15)
2 : keep.source term hide (Sweave-test-1.Rnw:17)
3 : echo keep.source print term verbatim (Sweave-test-1.Rnw:22)
4 : keep.source term verbatim (Sweave-test-1.Rnw:30)
5 : echo keep.source term verbatim (Sweave-test-1.Rnw:45)
6 : echo keep.source term verbatim pdf (Sweave-test-1.Rnw:53)
7 : echo keep.source term verbatim pdf (Sweave-test-1.Rnw:63)
You can now run (pdf)latex on 'Sweave-test-1.tex'
>
> ## This can be compiled to PDF by
> ## tools::texi2pdf("Sweave-test-1.tex")
> ## or outside R by
> ## R CMD texi2pdf Sweave-test-1.tex
> ## which sets the appropriate TEXINPUTS path.
> ## create an R source file from the code chunks
> Stangle(testfile)
Writing to file Sweave-test-1.R
> ## which can be sourced, e.g.
> source("Sweave-test-1.R")
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[1] 0.155650465 -0.657975138 1.200841208 -1.419381943 2.890727382
[6] 0.346590060 0.671430300 -0.599263622 0.854009909 -1.522505431
[11] -1.530834757 0.166325863 0.356764670 0.287575286 0.198010387
[16] -0.016157222 0.007170862 -0.702337419 1.111566895 0.043700483
One Sample t-test
data: x
t = 0.39719, df = 19, p-value = 0.6957
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.3932033 0.5773942
sample estimates:
mean of x
0.09209541
>
> ## Don't show:
> #if(!interactive()) unlink("Sweave-test-1*")
> ## End(Don't show)
>
>
>
>
>
> dev.off()
null device
1
>