The R Project SVN R

Rev

Rev 88907 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/utils/man/example.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
90299 maechler 3
% Copyright 1995-2026 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
86132 smeyer 11
  topic \code{topic}, with possible exceptions due to \verb{\dontrun},
12
  \verb{\dontshow}, and \verb{\donttest} tags, 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,
83594 maechler 17
        type = c("console", "html"), echo = TRUE,
81915 deepayan 18
        verbose = getOption("verbose"),
38151 ripley 19
        setRNG = FALSE, ask = getOption("example.ask"),
62809 murdoch 20
        prompt.prefix = abbreviate(topic, 6),
85268 maechler 21
        catch.aborts = FALSE,
67712 ripley 22
        run.dontrun = FALSE, run.donttest = interactive())
27442 ripley 23
}
24
\arguments{
25
  \item{topic}{name or literal character string: the online
26
    \code{\link{help}} topic the examples of which should be run.}
30304 hornik 27
  \item{package}{a character vector giving the package names to look
50140 ripley 28
    into for the topic, or \code{NULL} (the default), when all packages on
29
    the \link{search} path are used.}
27442 ripley 30
  \item{lib.loc}{a character vector of directory names of \R libraries,
31
    or \code{NULL}.  The default value of \code{NULL} corresponds to all
32
    libraries currently known.  If the default is used, the loaded
33
    packages are searched before the libraries.}
53976 maechler 34
  \item{character.only}{a logical indicating whether \code{topic} can be
35
    assumed to be a character string.}
36
  \item{give.lines}{logical: if true, the \emph{lines} of the example
37
    source code are returned as a character vector.}
27442 ripley 38
  \item{local}{logical: if \code{TRUE} evaluate locally, if \code{FALSE}
39
    evaluate in the workspace.}
81915 deepayan 40
  \item{type}{character: whether to show output in the console or a
41
    browser (using the dynamic help system). The latter is honored only
42
    in interactive sessions and if the \code{\link[knitr]{knitr}}
43
    package is installed. Several other arguments are silently ignored
44
    in that case, including \code{setRNG} and \code{lib.loc}.}
27442 ripley 45
  \item{echo}{logical;  if \code{TRUE}, show the \R input when sourcing.}
46
  \item{verbose}{logical;  if \code{TRUE}, show even more when running
47
    example code.}
48
  \item{setRNG}{logical or expression;  if not \code{FALSE}, the random
49
    number generator state is saved, then initialized to a specified state,
49612 hornik 50
    the example is run and the (saved) state is restored.
51
    \code{setRNG = TRUE} sets the same state as
65528 maechler 52
    \command{R CMD \link{check}} does for
27442 ripley 53
    running a package's examples.  This is currently equivalent to
90299 maechler 54
    \code{setRNG = \{RNGkind("default", "default", "default", "default"); set.seed(1)\}}.}
38151 ripley 55
  \item{ask}{logical (or \code{"default"}) indicating if
61160 ripley 56
    \code{\link{devAskNewPage}(ask = TRUE)} should be called
47940 murdoch 57
    before graphical output happens from the example code.  The value
58
    \code{"default"} (the factory-fresh default) means to ask if
83594 maechler 59
    \code{echo} is true and the graphics device appears to be
47940 murdoch 60
    interactive.  This parameter applies both to any currently opened
53976 maechler 61
    device and to any devices opened by the example code.}
39996 murdoch 62
  \item{prompt.prefix}{character; prefixes the prompt to be used if
83594 maechler 63
    \code{echo} is true (as it is by default).}
85268 maechler 64
  \item{catch.aborts}{logical, passed on to \code{\link{source}()},
85953 hornik 65
    indicating that \I{\dQuote{abort}ing} errors should be caught.}
62809 murdoch 66
  \item{run.dontrun}{logical indicating that \verb{\dontrun}
86132 smeyer 67
    should be ignored, i.e., do run the enclosed code.}
67712 ripley 68
  \item{run.donttest}{logical indicating that \verb{\donttest}
86132 smeyer 69
    should be ignored, i.e., do run the enclosed code.}
27442 ripley 70
}
32569 ripley 71
\value{
53976 maechler 72
  The value of the last evaluated expression, unless \code{give.lines}
73
  is true, where a \code{\link{character}} vector is returned.
32569 ripley 74
}
27442 ripley 75
\details{
76
  If \code{lib.loc} is not specified, the packages are searched for
50140 ripley 77
  amongst those already loaded, then in the libraries given by
78
  \code{\link{.libPaths}()}.  If \code{lib.loc} is specified, packages
79
  are searched for only in the specified libraries, even if they are
80
  already loaded from another library.  The search stops at the first
81
  package found that has help on the topic.
27442 ripley 82
 
83
  An attempt is made to load the package before running the examples,
84
  but this will not replace a package loaded from another location.
85
 
50140 ripley 86
  If \code{local = TRUE} objects are not created in the workspace and so
27442 ripley 87
  not available for examination after \code{example} completes: on the
50140 ripley 88
  other hand they cannot overwrite objects of the same name in the
27442 ripley 89
  workspace.
90
 
88907 smeyer 91
  As detailed in \manual{R-exts}{Documenting functions}, the author of
86132 smeyer 92
  the help page can tag parts of the examples with the following
93
  exception rules:
27442 ripley 94
  \describe{
86132 smeyer 95
    \item{\verb{\dontrun}}{encloses code that should not be run.}
96
    \item{\verb{\dontshow}}{encloses code that is invisible on help
27442 ripley 97
      pages, but will be run both by the package checking tools,
98
      and the \code{example()} function.  This was previously
86132 smeyer 99
      \verb{\testonly}, and that form is still accepted.}
100
    \item{\verb{\donttest}}{encloses code that typically should be run,
74831 hornik 101
      but not during package checking.  The default
102
      \code{run.donttest = \link{interactive}()}
103
      leads \code{example()} use in other help
67712 ripley 104
      page examples to skip \verb{\donttest} sections appropriately.}
32569 ripley 105
  }
86132 smeyer 106
  The additional \verb{\dontdiff} tag (in \R \eqn{\ge} 4.4.0) produces
107
  special comments in the code run by \code{example} (for
108
  \code{\link{Rdiff}}-based testing of example output), but does not
109
  affect which code is run or displayed on the help page.
27442 ripley 110
}
111
\author{Martin Maechler and others}
112
\seealso{\code{\link{demo}}}
113
\examples{
114
example(InsectSprays)
27496 ripley 115
## force use of the standard package 'stats':
61160 ripley 116
example("smooth", package = "stats", lib.loc = .Library)
27442 ripley 117
 
118
## set RNG *before* example as when R CMD check is run:
119
 
120
r1 <- example(quantile, setRNG = TRUE)
121
x1 <- rnorm(1)
122
u <- runif(1)
30630 ripley 123
## identical random numbers
27442 ripley 124
r2 <- example(quantile, setRNG = TRUE)
125
x2 <- rnorm(1)
126
stopifnot(identical(r1, r2))
127
## but x1 and x2 differ since the RNG state from before example()
128
## differs and is restored!
129
x1; x2
53976 maechler 130
 
131
## Exploring examples code:
132
## How large are the examples of "lm...()" functions?
61160 ripley 133
lmex <- sapply(apropos("^lm", mode = "function"),
134
               example, character.only = TRUE, give.lines = TRUE)
83594 maechler 135
lengths(lmex)
27442 ripley 136
}
137
\keyword{documentation}
138
\keyword{utilities}