| 27442 |
ripley |
1 |
\name{example}
|
|
|
2 |
\alias{example}
|
|
|
3 |
\title{Run an Examples Section from the Online Help}
|
|
|
4 |
\description{
|
|
|
5 |
Run all the \R code from the \bold{Examples} part of \R's online help
|
|
|
6 |
topic \code{topic} with two possible exceptions, \code{dontrun} and
|
|
|
7 |
\code{dontshow}, see Details below.
|
|
|
8 |
}
|
|
|
9 |
\usage{
|
|
|
10 |
example(topic, package = .packages(), lib.loc = NULL,
|
|
|
11 |
local = FALSE, echo = TRUE, verbose = getOption("verbose"),
|
|
|
12 |
setRNG = FALSE,
|
|
|
13 |
prompt.echo = paste(abbreviate(topic, 6),"> ", sep=""))
|
|
|
14 |
}
|
|
|
15 |
\arguments{
|
|
|
16 |
\item{topic}{name or literal character string: the online
|
|
|
17 |
\code{\link{help}} topic the examples of which should be run.}
|
|
|
18 |
\item{package}{a character vector with package names. By default,
|
|
|
19 |
all packages in the search path are used.}
|
|
|
20 |
\item{lib.loc}{a character vector of directory names of \R libraries,
|
|
|
21 |
or \code{NULL}. The default value of \code{NULL} corresponds to all
|
|
|
22 |
libraries currently known. If the default is used, the loaded
|
|
|
23 |
packages are searched before the libraries.}
|
|
|
24 |
\item{local}{logical: if \code{TRUE} evaluate locally, if \code{FALSE}
|
|
|
25 |
evaluate in the workspace.}
|
|
|
26 |
\item{echo}{logical; if \code{TRUE}, show the \R input when sourcing.}
|
|
|
27 |
\item{verbose}{logical; if \code{TRUE}, show even more when running
|
|
|
28 |
example code.}
|
|
|
29 |
\item{setRNG}{logical or expression; if not \code{FALSE}, the random
|
|
|
30 |
number generator state is saved, then initialized to a specified state,
|
|
|
31 |
the example is run and the (saved) state is restored. \code{setRNG
|
|
|
32 |
= TRUE} sets the same state as \code{R CMD \link{check}} does for
|
|
|
33 |
running a package's examples. This is currently equivalent to
|
|
|
34 |
\code{setRNG = \{RNGkind("default", "default"); set.seed(1)\}}.}
|
|
|
35 |
\item{prompt.echo}{character; gives the prompt to be used if
|
|
|
36 |
\code{echo = TRUE}.}
|
|
|
37 |
}
|
|
|
38 |
\value{(the value of the last evaluated expression).}
|
|
|
39 |
\details{
|
|
|
40 |
If \code{lib.loc} is not specified, the packages are searched for
|
|
|
41 |
amongst those already loaded, then in the specified libraries.
|
|
|
42 |
If \code{lib.loc} is specified, they are searched for only
|
|
|
43 |
in the specified libraries, even if they are already loaded from
|
|
|
44 |
another library.
|
|
|
45 |
|
|
|
46 |
An attempt is made to load the package before running the examples,
|
|
|
47 |
but this will not replace a package loaded from another location.
|
|
|
48 |
|
|
|
49 |
If \code{local=TRUE} objects are not created in the workspace and so
|
|
|
50 |
not available for examination after \code{example} completes: on the
|
|
|
51 |
other hand they cannot clobber objects of the same name in the
|
|
|
52 |
workspace.
|
|
|
53 |
|
|
|
54 |
As detailed in the manual \emph{Writing \R Extensions}, the author of
|
|
|
55 |
the help page can markup parts of the examples for two exception rules
|
|
|
56 |
\describe{
|
|
|
57 |
\item{\code{dontrun}}{encloses code that should not be run.}
|
|
|
58 |
\item{\code{dontshow}}{encloses code that is invisible on help
|
|
|
59 |
pages, but will be run both by the package checking tools,
|
|
|
60 |
and the \code{example()} function. This was previously
|
|
|
61 |
\code{testonly}, and that form is still accepted.}
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
\note{
|
|
|
65 |
The examples can be many small files. On some file systems it is
|
|
|
66 |
desirable to save space, and the files in the \file{R-ex} directory
|
|
|
67 |
of an installed package can be zipped up as a zip archive
|
|
|
68 |
\file{Rex.zip}.
|
|
|
69 |
}
|
|
|
70 |
\author{Martin Maechler and others}
|
|
|
71 |
\seealso{\code{\link{demo}}}
|
|
|
72 |
\examples{
|
|
|
73 |
example(InsectSprays)
|
| 27496 |
ripley |
74 |
## force use of the standard package 'stats':
|
|
|
75 |
example("smooth", package="stats", lib.loc=.Library)
|
| 27442 |
ripley |
76 |
|
|
|
77 |
## set RNG *before* example as when R CMD check is run:
|
|
|
78 |
|
|
|
79 |
r1 <- example(quantile, setRNG = TRUE)
|
|
|
80 |
x1 <- rnorm(1)
|
|
|
81 |
u <- runif(1)
|
|
|
82 |
## identical randum numbers
|
|
|
83 |
r2 <- example(quantile, setRNG = TRUE)
|
|
|
84 |
x2 <- rnorm(1)
|
|
|
85 |
stopifnot(identical(r1, r2))
|
|
|
86 |
## but x1 and x2 differ since the RNG state from before example()
|
|
|
87 |
## differs and is restored!
|
|
|
88 |
x1; x2
|
|
|
89 |
}
|
|
|
90 |
\keyword{documentation}
|
|
|
91 |
\keyword{utilities}
|