example {utils} | R Documentation |
Run all the R code from the Examples part of R's online help
topic topic
with two possible exceptions, dontrun
and
dontshow
, see ‘Details’ below.
example(topic, package = NULL, lib.loc = NULL,
local = FALSE, echo = TRUE,
verbose = getOption("verbose"),
setRNG = FALSE, ask = getOption("example.ask"),
prompt.prefix = abbreviate(topic, 6))
topic |
name or literal character string: the online
|
package |
a character vector giving the package names to look
into for example code, or |
lib.loc |
a character vector of directory names of R libraries,
or |
local |
logical: if |
echo |
logical; if |
verbose |
logical; if |
setRNG |
logical or expression; if not |
ask |
logical (or |
prompt.prefix |
character; prefixes the prompt to be used if
|
If lib.loc
is not specified, the packages are searched for
amongst those already loaded, then in the specified libraries.
If lib.loc
is specified, they are searched for only
in the specified libraries, even if they are already loaded from
another library.
An attempt is made to load the package before running the examples, but this will not replace a package loaded from another location.
If local=TRUE
objects are not created in the workspace and so
not available for examination after example
completes: on the
other hand they cannot clobber objects of the same name in the
workspace.
As detailed in the manual Writing R Extensions, the author of the help page can markup parts of the examples for two exception rules
dontrun
encloses code that should not be run.
dontshow
encloses code that is invisible on help
pages, but will be run both by the package checking tools,
and the example()
function. This was previously
testonly
, and that form is still accepted.
If the examples file contains non-ASCII characters the encoding used
will matter. If in a UTF-8 locale example
first tries UTF-8
and then Latin-1. (This can be overridden by setting the encoding in
the ‘.Rd’ file.)
The value of the last evaluated expression.
The examples can be many small files. On some file systems it is desirable to save space, and the files in the ‘R-ex’ directory of an installed package can be zipped up as a zip archive ‘Rex.zip’.
Martin Maechler and others
demo
example(InsectSprays)
## force use of the standard package 'stats':
example("smooth", package="stats", lib.loc=.Library)
## set RNG *before* example as when R CMD check is run:
r1 <- example(quantile, setRNG = TRUE)
x1 <- rnorm(1)
u <- runif(1)
## identical random numbers
r2 <- example(quantile, setRNG = TRUE)
x2 <- rnorm(1)
stopifnot(identical(r1, r2))
## but x1 and x2 differ since the RNG state from before example()
## differs and is restored!
x1; x2