The R Project SVN R

Rev

Rev 61160 | Rev 66951 | Go to most recent revision | 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
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 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
11
  topic \code{topic} with two possible exceptions, \code{dontrun} and
44184 ripley 12
  \code{dontshow}, 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),
20
        run.dontrun = FALSE)
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
53976 maechler 45
    \code{R CMD \link{check}} does for
27442 ripley 46
    running a package's examples.  This is currently equivalent to
47
    \code{setRNG = \{RNGkind("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.}
27442 ripley 59
}
32569 ripley 60
\value{
53976 maechler 61
  The value of the last evaluated expression, unless \code{give.lines}
62
  is true, where a \code{\link{character}} vector is returned.
32569 ripley 63
}
27442 ripley 64
\details{
65
  If \code{lib.loc} is not specified, the packages are searched for
50140 ripley 66
  amongst those already loaded, then in the libraries given by
67
  \code{\link{.libPaths}()}.  If \code{lib.loc} is specified, packages
68
  are searched for only in the specified libraries, even if they are
69
  already loaded from another library.  The search stops at the first
70
  package found that has help on the topic.
27442 ripley 71
 
72
  An attempt is made to load the package before running the examples,
73
  but this will not replace a package loaded from another location.
74
 
50140 ripley 75
  If \code{local = TRUE} objects are not created in the workspace and so
27442 ripley 76
  not available for examination after \code{example} completes: on the
50140 ripley 77
  other hand they cannot overwrite objects of the same name in the
27442 ripley 78
  workspace.
79
 
80
  As detailed in the manual \emph{Writing \R Extensions}, the author of
81
  the help page can markup parts of the examples for two exception rules
82
  \describe{
83
    \item{\code{dontrun}}{encloses code that should not be run.}
84
    \item{\code{dontshow}}{encloses code that is invisible on help
85
      pages, but will be run both by the package checking tools,
86
      and the \code{example()} function.  This was previously
87
      \code{testonly}, and that form is still accepted.}
32569 ripley 88
  }
27442 ripley 89
}
90
\author{Martin Maechler and others}
91
\seealso{\code{\link{demo}}}
92
\examples{
93
example(InsectSprays)
27496 ripley 94
## force use of the standard package 'stats':
61160 ripley 95
example("smooth", package = "stats", lib.loc = .Library)
27442 ripley 96
 
97
## set RNG *before* example as when R CMD check is run:
98
 
99
r1 <- example(quantile, setRNG = TRUE)
100
x1 <- rnorm(1)
101
u <- runif(1)
30630 ripley 102
## identical random numbers
27442 ripley 103
r2 <- example(quantile, setRNG = TRUE)
104
x2 <- rnorm(1)
105
stopifnot(identical(r1, r2))
106
## but x1 and x2 differ since the RNG state from before example()
107
## differs and is restored!
108
x1; x2
53976 maechler 109
 
110
## Exploring examples code:
111
## How large are the examples of "lm...()" functions?
61160 ripley 112
lmex <- sapply(apropos("^lm", mode = "function"),
113
               example, character.only = TRUE, give.lines = TRUE)
53976 maechler 114
sapply(lmex, length)
27442 ripley 115
}
116
\keyword{documentation}
117
\keyword{utilities}