| Line 1... |
Line 1... |
| 1 |
% File src/library/utils/man/Rtangle.Rd
|
1 |
% File src/library/utils/man/Rtangle.Rd
|
| 2 |
% Part of the R package, https://www.R-project.org
|
2 |
% Part of the R package, https://www.R-project.org
|
| 3 |
% Copyright 1995-2016 R Core Team
|
3 |
% Copyright 1995-2026 R Core Team
|
| 4 |
% Distributed under GPL 2 or later
|
4 |
% Distributed under GPL 2 or later
|
| 5 |
|
5 |
|
| 6 |
\name{Rtangle}
|
6 |
\name{Rtangle}
|
| 7 |
\alias{Rtangle}
|
7 |
\alias{Rtangle}
|
| 8 |
\alias{RtangleSetup}
|
8 |
\alias{RtangleSetup}
|
| 9 |
\alias{RtangleRuncode}
|
9 |
\alias{RtangleRuncode}
|
| 10 |
\alias{RtangleWritedoc}
|
10 |
\alias{RtangleWritedoc}
|
| 11 |
\alias{RtangleFinish}
|
11 |
\alias{RtangleFinish}
|
| - |
|
12 |
\alias{RtangleOptions}
|
| 12 |
\title{R Driver for \I{Stangle}}
|
13 |
\title{R Driver for \I{Stangle}}
|
| 13 |
\description{
|
14 |
\description{
|
| 14 |
A driver for \code{\link{Stangle}} that extracts \R code chunks.
|
15 |
A driver for \code{\link{Stangle}} that extracts \R code chunks.
|
| 15 |
}
|
16 |
}
|
| 16 |
\usage{
|
17 |
\usage{
|
| 17 |
Rtangle()
|
18 |
Rtangle()
|
| 18 |
|
19 |
|
| 19 |
RtangleSetup(file, syntax, output = NULL, annotate = TRUE,
|
20 |
RtangleSetup(file, syntax, output = NULL, annotate = TRUE,
|
| 20 |
split = FALSE, quiet = FALSE, drop.evalFALSE = FALSE, \dots)
|
21 |
split = FALSE, quiet = FALSE, drop.evalFALSE = FALSE,
|
| - |
|
22 |
chunk.sep = "\n\n", \dots)
|
| 21 |
|
23 |
|
| 22 |
RtangleRuncode(object, chunk, options)
|
24 |
RtangleRuncode(object, chunk, options)
|
| 23 |
|
25 |
|
| 24 |
RtangleWritedoc(object, chunk)
|
26 |
RtangleWritedoc(object, chunk)
|
| 25 |
|
27 |
|
| 26 |
RtangleFinish(object, error = FALSE)
|
28 |
RtangleFinish(object, error = FALSE)
|
| - |
|
29 |
|
| - |
|
30 |
RtangleOptions(options)
|
| 27 |
}
|
31 |
}
|
| 28 |
\arguments{
|
32 |
\arguments{
|
| 29 |
\item{file}{name of Sweave source file. See the description of the
|
33 |
\item{file}{name of Sweave source file. See the description of the
|
| 30 |
corresponding argument of \code{\link{Stangle}}.}
|
34 |
corresponding argument of \code{\link{Stangle}}.}
|
| 31 |
\item{syntax}{an object of class \code{SweaveSyntax}.}
|
35 |
\item{syntax}{an object of class \code{SweaveSyntax}.}
|
| Line 33... |
Line 37... |
| 33 |
\sQuote{Details}.}
|
37 |
\sQuote{Details}.}
|
| 34 |
\item{annotate}{a logical or \code{\link{function}}. When \code{TRUE}
|
38 |
\item{annotate}{a logical or \code{\link{function}}. When \code{TRUE}
|
| 35 |
code chunks are separated by comment lines specifying the names and
|
39 |
code chunks are separated by comment lines specifying the names and
|
| 36 |
line numbers of the code chunks. If \code{FALSE} the decorating
|
40 |
line numbers of the code chunks. If \code{FALSE} the decorating
|
| 37 |
comments are omitted. Alternatively, \code{annotate} may be a
|
41 |
comments are omitted. Alternatively, \code{annotate} may be a
|
| 38 |
function, see section \sQuote{Chunk annotation}.}
|
42 |
function, see section \sQuote{Chunk annotation}.}
|
| 39 |
\item{split}{split output into a file for each code chunk?}
|
43 |
\item{split}{split output into a file for each code chunk?}
|
| 40 |
\item{quiet}{logical to suppress all progress messages.}
|
44 |
\item{quiet}{logical to suppress all progress messages.}
|
| 41 |
\item{drop.evalFALSE}{logical. When \code{FALSE} all chunks with
|
45 |
\item{drop.evalFALSE}{logical. When \code{FALSE} all chunks with
|
| 42 |
option \code{eval = FALSE} are \emph{commented out} in the output;
|
46 |
option \code{eval = FALSE} are \emph{commented out} in the output;
|
| 43 |
otherwise (\code{drop.evalFALSE = TRUE}) they are omitted entirely.}
|
47 |
otherwise (\code{drop.evalFALSE = TRUE}) they are omitted entirely.}
|
| - |
|
48 |
\item{chunk.sep}{character string or logical. Text to use as separator
|
| - |
|
49 |
between code chunks. If \code{TRUE}, the default value is used. If
|
| - |
|
50 |
\code{FALSE}, no separator is added between chunks. Allowed (and
|
| - |
|
51 |
specially useful) as a chunk option in the file.}
|
| 44 |
\item{\dots}{additional named arguments setting defaults for further
|
52 |
\item{\dots}{additional named arguments setting defaults for further
|
| 45 |
options listed in \sQuote{Supported Options}.}
|
53 |
options listed in \sQuote{Supported Options}.}
|
| 46 |
\item{object}{an object created by the \code{setup} function of the
|
54 |
\item{object}{an object created by the \code{setup} function of the
|
| 47 |
driver.}
|
55 |
driver.}
|
| 48 |
\item{chunk}{text of a code chunk.}
|
56 |
\item{chunk}{text of a code chunk.}
|
| Line 51... |
Line 59... |
| 51 |
\code{\link{Sweave}}.}
|
59 |
\code{\link{Sweave}}.}
|
| 52 |
}
|
60 |
}
|
| 53 |
\details{
|
61 |
\details{
|
| 54 |
See \code{\link{RweaveLatex}} for details on Sweave drivers.
|
62 |
See \code{\link{RweaveLatex}} for details on Sweave drivers.
|
| 55 |
|
63 |
|
| 56 |
\code{Rtangle} is the \I{Stangle} driver.
|
64 |
\code{Rtangle} is the \I{Stangle} driver. \code{RtangleSetup},
|
| 57 |
\code{RtangleSetup}, \code{RtangleRuncode},
|
65 |
\code{RtangleRuncode}, \code{RtangleWritedoc}, \code{RtangleFinish}
|
| 58 |
\code{RtangleWritedoc} and \code{RtangleFinish}
|
66 |
and \code{RtangleOptions} are functions for the \code{setup},
|
| 59 |
are functions for the
|
- |
|
| 60 |
\code{setup}, \code{runcode}, \code{writedoc} and \code{finish}
|
67 |
\code{runcode}, \code{writedoc}, \code{finish} and \code{checkopts}
|
| 61 |
elements of the driver, respectively.
|
68 |
elements of the driver, respectively.
|
| 62 |
The \code{checkopts} element uses the function
|
- |
|
| 63 |
\code{\link{RweaveLatexOptions}} of the \code{RweaveLatex} driver.
|
- |
|
| 64 |
|
69 |
|
| 65 |
All \code{RtangleSetup} arguments may be used as arguments in the
|
70 |
All \code{RtangleSetup} arguments may be used as arguments in the
|
| 66 |
\code{\link{Stangle}} call.
|
71 |
\code{\link{Stangle}} call.
|
| 67 |
|
72 |
|
| 68 |
Unless \code{split = TRUE}, the default name of the output file is
|
73 |
Unless \code{split = TRUE}, the default name of the output file is
|
| 69 |
\code{basename(file)} with an extension corresponding to the Sweave
|
74 |
\code{basename(file)} with an extension corresponding to the Sweave
|
| 70 |
syntax (e.g., \file{Rnw}, \file{Stex}) replaced by \file{R}. File
|
75 |
syntax (e.g., \file{Rnw}, \file{Stex}) replaced by \file{R}. File
|
| 71 |
names \code{"stdout"} and \code{"stderr"} are interpreted as the
|
76 |
names \code{"stdout"} and \code{"stderr"} are interpreted as the
|
| 72 |
output and message connection respectively.
|
77 |
output and message connection respectively.
|
| 73 |
|
78 |
|
| 74 |
If splitting is selected (including by the options in the file), each
|
79 |
If splitting is selected (including by the options in the file), each
|
| 75 |
chunk is written to a separate file with extension the name of the
|
80 |
chunk is written to a separate file with extension as specified by the
|
| - |
|
81 |
chunk option \code{extension} (by default the \sQuote{engine}, usually
|
| - |
|
82 |
\file{.R}).
|
| - |
|
83 |
|
| - |
|
84 |
The chunk separator is added \emph{before} all chunks, except the
|
| - |
|
85 |
first one. Therefore, no chunk separator follows the last chunk. No
|
| 76 |
\sQuote{engine} (default \file{.R}).
|
86 |
newline (aka \sQuote{line feed}) is added to the value of the
|
| - |
|
87 |
argument.
|
| 77 |
|
88 |
|
| 78 |
Note that this driver does more than simply extract the code chunks
|
89 |
Note that this driver does more than simply extract the code chunks
|
| 79 |
verbatim, because chunks may re-use earlier chunks.
|
90 |
verbatim, because chunks may re-use earlier chunks.
|
| 80 |
}
|
91 |
}
|
| 81 |
|
92 |
|
| Line 102... |
Line 113... |
| 102 |
the default.
|
113 |
the default.
|
| 103 |
}
|
114 |
}
|
| 104 |
|
115 |
|
| 105 |
\section{Supported Options}{
|
116 |
\section{Supported Options}{
|
| 106 |
\code{Rtangle} supports the following options for code chunks (the
|
117 |
\code{Rtangle} supports the following options for code chunks (the
|
| 107 |
values in parentheses show the default values):
|
118 |
values in parentheses show the default values). Character string
|
| - |
|
119 |
values should be quoted when passed from \code{\link{Stangle}} through
|
| - |
|
120 |
\code{\dots} but not when used in the header of a code chunk.
|
| 108 |
\describe{
|
121 |
\describe{
|
| - |
|
122 |
\item{extension:}{logical (TRUE) or character string. Extension to
|
| - |
|
123 |
use for the file name, without the leading dot, when \code{split =
|
| - |
|
124 |
TRUE}. The default extension is the value of \sQuote{engine}. If
|
| - |
|
125 |
\code{FALSE}, no extension is added to the file name.}
|
| - |
|
126 |
|
| 109 |
\item{engine:}{character string (\code{"R"}). Only chunks with
|
127 |
\item{engine:}{character string (\code{"R"}). Only chunks with
|
| 110 |
\code{engine} equal to \code{"R"} or \code{"S"} are processed.}
|
128 |
\code{engine} equal to \code{"R"} or \code{"S"} are processed.}
|
| 111 |
|
129 |
|
| 112 |
\item{keep.source:}{logical (\code{TRUE}). If
|
130 |
\item{keep.source:}{logical (\code{TRUE}). If
|
| 113 |
\code{keep.source == TRUE} the original source is copied to the
|
131 |
\code{keep.source == TRUE} the original source is copied to the
|
| 114 |
file. Otherwise, deparsed source is output.}
|
132 |
file. Otherwise, deparsed source is output.}
|
| 115 |
|
133 |
|
| 116 |
\item{eval:}{logical (\code{TRUE}). If \code{FALSE}, the code chunk
|
134 |
\item{eval:}{logical (\code{TRUE}). If \code{FALSE}, the code chunk
|
| 117 |
is copied across but commented out.}
|
135 |
is copied across but commented out.}
|
| 118 |
|
136 |
|
| - |
|
137 |
\item{ignore.on.tangle:}{logical (FALSE). If \code{TRUE}, the code
|
| - |
|
138 |
chunk is ignored entirely. Chunks ignored on tangling are not
|
| - |
|
139 |
written out to script files, but remain processed normally on
|
| - |
|
140 |
weaving (unless \code{ignore = TRUE}). Therefore, chunks may still
|
| - |
|
141 |
use \code{eval = TRUE} for weaving purposes, yet not be tangled.}
|
| - |
|
142 |
|
| - |
|
143 |
\item{ignore:}{logical (FALSE). An alternative way to set both
|
| - |
|
144 |
\code{ignore.on.weave} of \code{\link{RweaveLatex}} and
|
| - |
|
145 |
\code{ignore.on.tangle}.}
|
| - |
|
146 |
|
| - |
|
147 |
\item{tangle:}{An alternative way to specify \code{!ignore.on.tangle}.}
|
| - |
|
148 |
|
| 119 |
\item{prefix}{Used if \code{split = TRUE}. See \code{prefix.string}.}
|
149 |
\item{prefix:}{Used if \code{split = TRUE}. See \code{prefix.string}.}
|
| 120 |
|
150 |
|
| 121 |
\item{prefix.string:}{a character string, default is the name of the
|
151 |
\item{prefix.string:}{a character string, default is the name of the
|
| 122 |
source file (without extension). Used if \code{split = TRUE} as
|
152 |
source file (without extension). Used if \code{split = TRUE} as
|
| 123 |
the prefix for the filename if the chunk has no label, or if it
|
153 |
the prefix for the filename if the chunk has no label, or if it
|
| 124 |
has a label and \code{prefix = TRUE}. Note that this is used as
|
154 |
has a label and \code{prefix = TRUE}. Note that this is used as
|
| Line 126... |
Line 156... |
| 126 |
|
156 |
|
| 127 |
\item{show.line.nos}{logical (\code{FALSE}). Should the output be
|
157 |
\item{show.line.nos}{logical (\code{FALSE}). Should the output be
|
| 128 |
annotated with comments showing the line number of the first code line
|
158 |
annotated with comments showing the line number of the first code line
|
| 129 |
of the chunk?}
|
159 |
of the chunk?}
|
| 130 |
}
|
160 |
}
|
| - |
|
161 |
|
| - |
|
162 |
Option \code{extension} is specially useful with \code{ignore.on.weave
|
| - |
|
163 |
= TRUE} of \code{\link{RweaveLatex}} to include code or text that the
|
| - |
|
164 |
engine would not be able to parse.
|
| - |
|
165 |
|
| - |
|
166 |
Note that options may not take their values from objects on tangling,
|
| - |
|
167 |
since code chunks are never evaluated. For options set this way for
|
| - |
|
168 |
weaving purposes, the driver will use the default value, with a
|
| - |
|
169 |
warning.
|
| 131 |
}
|
170 |
}
|
| 132 |
|
171 |
|
| 133 |
\author{
|
172 |
\author{
|
| 134 |
Friedrich Leisch and R-core, with contributions by Vincent Goulet.
|
173 |
Friedrich Leisch and R-core, with contributions by Vincent Goulet.
|
| 135 |
}
|
174 |
}
|
| 136 |
\seealso{
|
175 |
\seealso{
|
| 137 |
\sQuote{\href{../doc/Sweave.pdf}{Sweave User Manual}}, a vignette in
|
176 |
\sQuote{\href{../doc/Sweave.pdf}{Sweave User Manual}}, a vignette in
|
| 138 |
the \pkg{utils} package.%% ../vignettes/Sweave.Rnw
|
177 |
the \pkg{utils} package.%% ../vignettes/Sweave.Rnw
|
| 139 |
|
178 |
|
| 140 |
\code{\link{Sweave}}, \code{\link{RweaveLatex}}
|
179 |
\code{\link{Sweave}}, \code{\link{RweaveLatex}}
|
| 141 |
}
|
180 |
}
|
| 142 |
\examples{
|
181 |
\examples{
|
| - |
|
182 |
## Simple example
|
| 143 |
nmRnw <- "example-1.Rnw"
|
183 |
nmRnw <- "example-1.Rnw"
|
| 144 |
exfile <- system.file("Sweave", nmRnw, package = "utils")
|
184 |
exfile <- system.file("Sweave", nmRnw, package = "utils")
|
| - |
|
185 |
|
| 145 |
## Create R source file
|
186 |
## Check the contents of the file
|
| - |
|
187 |
if (interactive()) file.show(exfile)
|
| - |
|
188 |
|
| - |
|
189 |
## Tangle the file in the current working directory with the
|
| - |
|
190 |
## default options
|
| 146 |
Stangle(exfile)
|
191 |
Stangle(exfile)
|
| - |
|
192 |
|
| - |
|
193 |
## View the R script
|
| 147 |
nmR <- sub("Rnw$", "R", nmRnw) # the (default) R output file name
|
194 |
nmR <- sub("Rnw$", "R", nmRnw) # default R output file name
|
| - |
|
195 |
if (interactive()) file.show(nmR)
|
| - |
|
196 |
\dontshow{file.rename(nmR, sub(".R$", "-default.R", nmR))}
|
| - |
|
197 |
## Reduce spacing between code chunks to a single blank line
|
| - |
|
198 |
Stangle(exfile, chunk.sep = "\n")
|
| 148 |
if(interactive()) file.show("example-1.R")
|
199 |
if (interactive()) file.show(nmR)
|
| 149 |
\dontshow{file.rename("example-1.R", "example-1_def.R")}
|
200 |
\dontshow{file.rename(nmR, sub(".R$", "-single.R", nmR))}
|
| 150 |
## Smaller R source file with custom annotation:
|
201 |
## Repeat with custom annotations
|
| 151 |
my.Ann <- function(options, chunk, output) {
|
202 |
my.Ann <- function(options, chunk, output) {
|
| 152 |
cat("### chunk #", options$chunknr, ": ",
|
203 |
cat("### chunk #", options$chunknr, ": ",
|
| 153 |
if(!is.null(ol <- options$label)) ol else .RtangleCodeLabel(chunk),
|
204 |
options$label \%||\% .RtangleCodeLabel(chunk),
|
| 154 |
if(!options$eval) " (eval = FALSE)", "\n",
|
205 |
if (!options$eval) " (eval = FALSE)", "\n",
|
| 155 |
file = output, sep = "")
|
206 |
file = output, sep = "")
|
| 156 |
}
|
207 |
}
|
| 157 |
Stangle(exfile, annotate = my.Ann)
|
208 |
Stangle(exfile, annotate = my.Ann, chunk.sep = "\n")
|
| - |
|
209 |
if (interactive()) file.show(nmR)
|
| - |
|
210 |
\dontshow{file.rename(nmR, sub(".R$", "-myAnn.R", nmR))}
|
| - |
|
211 |
## Options taking their values from objects for weaving
|
| - |
|
212 |
## purposes instead use their default value, with a warning,
|
| - |
|
213 |
## on tangling
|
| - |
|
214 |
nmRnw <- "example-2.Rnw"
|
| - |
|
215 |
exfile <- system.file("Sweave", nmRnw, package = "utils")
|
| - |
|
216 |
if (interactive()) file.show(exfile)
|
| - |
|
217 |
Stangle(exfile)
|
| - |
|
218 |
if (interactive()) file.show(sub("Rnw$", "R", nmRnw))
|
| - |
|
219 |
|
| - |
|
220 |
## More advanced example with finer control of output
|
| - |
|
221 |
nmRnw <- "example-3.Rnw"
|
| - |
|
222 |
exfile <- system.file("Sweave", nmRnw, package = "utils")
|
| 158 |
if(interactive()) file.show("example-1.R")
|
223 |
if (interactive()) file.show(exfile)
|
| - |
|
224 |
Stangle(exfile)
|
| - |
|
225 |
|
| - |
|
226 |
## Note in the R script: chunk #1 commented out; invalid R
|
| - |
|
227 |
## code in chunks #2, #4 and #5 extracted verbatim; no
|
| - |
|
228 |
## separator between chunks #2 and #3; last chunk left out
|
| - |
|
229 |
## of the script
|
| - |
|
230 |
nmR <- sub("Rnw$", "R", nmRnw)
|
| - |
|
231 |
if (interactive()) file.show(nmR)
|
| - |
|
232 |
|
| - |
|
233 |
## The effect of 'chunk.sep=FALSE' in chunk #3 is more
|
| - |
|
234 |
## interesting without the annotations
|
| - |
|
235 |
Stangle(exfile, annotate = FALSE, chunk.sep = "\n")
|
| - |
|
236 |
if (interactive()) file.show(nmR)
|
| 159 |
\dontshow{file.rename("example-1.R", "example-1_myA.R")}
|
237 |
\dontshow{file.rename(nmR, sub(".R$", "-noAnn.R", nmR))}
|
| - |
|
238 |
## With 'drop.evalFALSE = TRUE', unevaluated chunks are
|
| - |
|
239 |
## entirely dropped from the tangled script
|
| 160 |
Stangle(exfile, annotate = my.Ann, drop.evalFALSE=TRUE)
|
240 |
Stangle(exfile, annotate = FALSE, drop.evalFALSE = TRUE)
|
| 161 |
if(interactive()) file.show("example-1.R")
|
241 |
if (interactive()) file.show(nmR)
|
| 162 |
\dontshow{file.rename("example-1.R", "example-1_myA-noF.R")}
|
242 |
\dontshow{file.rename(nmR, sub(".R$", "-noAnn-noF.R", nmR))}
|
| - |
|
243 |
## Tangling with 'split=TRUE' extracts the shell script in
|
| - |
|
244 |
## chunk #4 to a file 'hello.sh', and the text file in
|
| - |
|
245 |
## chunk #5 to a file 'README'
|
| - |
|
246 |
Stangle(exfile, split = TRUE, annotate = FALSE)
|
| - |
|
247 |
if (interactive()) file.show("hello.sh")
|
| - |
|
248 |
if (interactive()) file.show("README")
|
| 163 |
}
|
249 |
}
|
| 164 |
\keyword{utilities}
|
250 |
\keyword{utilities}
|