
Last chance! 50% off unlimited learning
Sale ends in
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.
Sweave(file, driver = RweaveLatex(), syntax = getOption("SweaveSyntax"), encoding = "", ...)
Stangle(file, driver = Rtangle(), syntax = getOption("SweaveSyntax"), encoding = "", ...)
file
.NULL
or an object of class SweaveSyntax
or
a character string with its name.
See the section ‘Syntax Definition’.file
.RweaveLatex
and
Rtangle
.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.*
. 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 \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}cacheSweavecacheSweave 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"
.
Packages \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}cacheSweavecacheSweave, weaver (Bioconductor) and \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}SweaveListingUtilsSweaveListingUtils.
Further Sweave drivers are in, for example, packages \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}R2HTMLR2HTML,
\href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}asciiascii, \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}odfWeaveodfWeave and \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}pgfSweavepgfSweave.
Non-Sweave vignettes may be built with tools::buildVignette
.
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
unix
## R CMD texi2pdf Sweave-test-1.tex
## which sets the appropriate TEXINPUTS path.
windows
## Rcmd texify --pdf Sweave-test-1.tex
## if MiKTeX is available.
## create an R source file from the code chunks
Stangle(testfile)
## which can be sourced, e.g.
source("Sweave-test-1.R")
Run the code above in your browser using DataLab