| 42333 |
ripley |
1 |
% File src/library/utils/man/example.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 76160 |
luke |
3 |
% Copyright 1995-2019 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 27442 |
ripley |
6 |
\name{example}
|
| 56186 |
murdoch |
7 |
\alias{example}
|
| 27442 |
ripley |
8 |
\title{Run an Examples Section from the Online Help}
|
|
|
9 |
\description{
|
|
|
10 |
Run all the \R code from the \bold{Examples} part of \R's online help
|
| 66951 |
maechler |
11 |
topic \code{topic} with possible exceptions \code{dontrun},
|
| 67712 |
ripley |
12 |
\code{dontshow}, and \code{donttest}, see \sQuote{Details} below.
|
| 27442 |
ripley |
13 |
}
|
|
|
14 |
\usage{
|
| 30304 |
hornik |
15 |
example(topic, package = NULL, lib.loc = NULL,
|
| 53976 |
maechler |
16 |
character.only = FALSE, give.lines = FALSE, local = FALSE,
|
|
|
17 |
echo = TRUE, verbose = getOption("verbose"),
|
| 38151 |
ripley |
18 |
setRNG = FALSE, ask = getOption("example.ask"),
|
| 62809 |
murdoch |
19 |
prompt.prefix = abbreviate(topic, 6),
|
| 67712 |
ripley |
20 |
run.dontrun = FALSE, run.donttest = interactive())
|
| 27442 |
ripley |
21 |
}
|
|
|
22 |
\arguments{
|
|
|
23 |
\item{topic}{name or literal character string: the online
|
|
|
24 |
\code{\link{help}} topic the examples of which should be run.}
|
| 30304 |
hornik |
25 |
\item{package}{a character vector giving the package names to look
|
| 50140 |
ripley |
26 |
into for the topic, or \code{NULL} (the default), when all packages on
|
|
|
27 |
the \link{search} path are used.}
|
| 27442 |
ripley |
28 |
\item{lib.loc}{a character vector of directory names of \R libraries,
|
|
|
29 |
or \code{NULL}. The default value of \code{NULL} corresponds to all
|
|
|
30 |
libraries currently known. If the default is used, the loaded
|
|
|
31 |
packages are searched before the libraries.}
|
| 53976 |
maechler |
32 |
\item{character.only}{a logical indicating whether \code{topic} can be
|
|
|
33 |
assumed to be a character string.}
|
|
|
34 |
\item{give.lines}{logical: if true, the \emph{lines} of the example
|
|
|
35 |
source code are returned as a character vector.}
|
| 27442 |
ripley |
36 |
\item{local}{logical: if \code{TRUE} evaluate locally, if \code{FALSE}
|
|
|
37 |
evaluate in the workspace.}
|
|
|
38 |
\item{echo}{logical; if \code{TRUE}, show the \R input when sourcing.}
|
|
|
39 |
\item{verbose}{logical; if \code{TRUE}, show even more when running
|
|
|
40 |
example code.}
|
|
|
41 |
\item{setRNG}{logical or expression; if not \code{FALSE}, the random
|
|
|
42 |
number generator state is saved, then initialized to a specified state,
|
| 49612 |
hornik |
43 |
the example is run and the (saved) state is restored.
|
|
|
44 |
\code{setRNG = TRUE} sets the same state as
|
| 65528 |
maechler |
45 |
\command{R CMD \link{check}} does for
|
| 27442 |
ripley |
46 |
running a package's examples. This is currently equivalent to
|
| 76160 |
luke |
47 |
\code{setRNG = \{RNGkind("default", "default", "default"); set.seed(1)\}}.}
|
| 38151 |
ripley |
48 |
\item{ask}{logical (or \code{"default"}) indicating if
|
| 61160 |
ripley |
49 |
\code{\link{devAskNewPage}(ask = TRUE)} should be called
|
| 47940 |
murdoch |
50 |
before graphical output happens from the example code. The value
|
|
|
51 |
\code{"default"} (the factory-fresh default) means to ask if
|
|
|
52 |
\code{echo == TRUE} and the graphics device appears to be
|
|
|
53 |
interactive. This parameter applies both to any currently opened
|
| 53976 |
maechler |
54 |
device and to any devices opened by the example code.}
|
| 39996 |
murdoch |
55 |
\item{prompt.prefix}{character; prefixes the prompt to be used if
|
| 27442 |
ripley |
56 |
\code{echo = TRUE}.}
|
| 62809 |
murdoch |
57 |
\item{run.dontrun}{logical indicating that \verb{\dontrun}
|
|
|
58 |
should be ignored.}
|
| 67712 |
ripley |
59 |
\item{run.donttest}{logical indicating that \verb{\donttest}
|
| 66951 |
maechler |
60 |
should be ignored.}
|
| 27442 |
ripley |
61 |
}
|
| 32569 |
ripley |
62 |
\value{
|
| 53976 |
maechler |
63 |
The value of the last evaluated expression, unless \code{give.lines}
|
|
|
64 |
is true, where a \code{\link{character}} vector is returned.
|
| 32569 |
ripley |
65 |
}
|
| 27442 |
ripley |
66 |
\details{
|
|
|
67 |
If \code{lib.loc} is not specified, the packages are searched for
|
| 50140 |
ripley |
68 |
amongst those already loaded, then in the libraries given by
|
|
|
69 |
\code{\link{.libPaths}()}. If \code{lib.loc} is specified, packages
|
|
|
70 |
are searched for only in the specified libraries, even if they are
|
|
|
71 |
already loaded from another library. The search stops at the first
|
|
|
72 |
package found that has help on the topic.
|
| 27442 |
ripley |
73 |
|
|
|
74 |
An attempt is made to load the package before running the examples,
|
|
|
75 |
but this will not replace a package loaded from another location.
|
|
|
76 |
|
| 50140 |
ripley |
77 |
If \code{local = TRUE} objects are not created in the workspace and so
|
| 27442 |
ripley |
78 |
not available for examination after \code{example} completes: on the
|
| 50140 |
ripley |
79 |
other hand they cannot overwrite objects of the same name in the
|
| 27442 |
ripley |
80 |
workspace.
|
|
|
81 |
|
|
|
82 |
As detailed in the manual \emph{Writing \R Extensions}, the author of
|
| 66951 |
maechler |
83 |
the help page can markup parts of the examples for exception rules
|
| 27442 |
ripley |
84 |
\describe{
|
|
|
85 |
\item{\code{dontrun}}{encloses code that should not be run.}
|
|
|
86 |
\item{\code{dontshow}}{encloses code that is invisible on help
|
|
|
87 |
pages, but will be run both by the package checking tools,
|
|
|
88 |
and the \code{example()} function. This was previously
|
|
|
89 |
\code{testonly}, and that form is still accepted.}
|
| 67712 |
ripley |
90 |
\item{\code{donttest}}{encloses code that typically should be run,
|
| 74831 |
hornik |
91 |
but not during package checking. The default
|
|
|
92 |
\code{run.donttest = \link{interactive}()}
|
|
|
93 |
leads \code{example()} use in other help
|
| 67712 |
ripley |
94 |
page examples to skip \verb{\donttest} sections appropriately.}
|
| 32569 |
ripley |
95 |
}
|
| 27442 |
ripley |
96 |
}
|
|
|
97 |
\author{Martin Maechler and others}
|
|
|
98 |
\seealso{\code{\link{demo}}}
|
|
|
99 |
\examples{
|
|
|
100 |
example(InsectSprays)
|
| 27496 |
ripley |
101 |
## force use of the standard package 'stats':
|
| 61160 |
ripley |
102 |
example("smooth", package = "stats", lib.loc = .Library)
|
| 27442 |
ripley |
103 |
|
|
|
104 |
## set RNG *before* example as when R CMD check is run:
|
|
|
105 |
|
|
|
106 |
r1 <- example(quantile, setRNG = TRUE)
|
|
|
107 |
x1 <- rnorm(1)
|
|
|
108 |
u <- runif(1)
|
| 30630 |
ripley |
109 |
## identical random numbers
|
| 27442 |
ripley |
110 |
r2 <- example(quantile, setRNG = TRUE)
|
|
|
111 |
x2 <- rnorm(1)
|
|
|
112 |
stopifnot(identical(r1, r2))
|
|
|
113 |
## but x1 and x2 differ since the RNG state from before example()
|
|
|
114 |
## differs and is restored!
|
|
|
115 |
x1; x2
|
| 53976 |
maechler |
116 |
|
|
|
117 |
## Exploring examples code:
|
|
|
118 |
## How large are the examples of "lm...()" functions?
|
| 61160 |
ripley |
119 |
lmex <- sapply(apropos("^lm", mode = "function"),
|
|
|
120 |
example, character.only = TRUE, give.lines = TRUE)
|
| 53976 |
maechler |
121 |
sapply(lmex, length)
|
| 27442 |
ripley |
122 |
}
|
|
|
123 |
\keyword{documentation}
|
|
|
124 |
\keyword{utilities}
|