example                package:utils                R Documentation

_R_u_n _a_n _E_x_a_m_p_l_e_s _S_e_c_t_i_o_n _f_r_o_m _t_h_e _O_n_l_i_n_e _H_e_l_p

_D_e_s_c_r_i_p_t_i_o_n:

     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.

_U_s_a_g_e:

     example(topic, package = NULL, lib.loc = NULL,
             local = FALSE, echo = TRUE, verbose = getOption("verbose"),
             setRNG = FALSE,
             prompt.echo = paste(abbreviate(topic, 6),"> ", sep=""))

_A_r_g_u_m_e_n_t_s:

   topic: name or literal character string: the online 'help' topic the
          examples of which should be run.

 package: a character vector giving the package names to look into for
          example code, or 'NULL'.  By default, all packages in the
          search path are used.

 lib.loc: a character vector of directory names of R libraries, or
          'NULL'.  The default value of 'NULL' corresponds to all
          libraries currently known.  If the default is used, the
          loaded packages are searched before the libraries.

   local: logical: if 'TRUE' evaluate locally, if 'FALSE' evaluate in
          the workspace.

    echo: logical;  if 'TRUE', show the R input when sourcing.

 verbose: logical;  if 'TRUE', show even more when running example
          code.

  setRNG: logical or expression;  if not 'FALSE', the random number
          generator state is saved, then initialized to a specified
          state, the example is run and the (saved) state is restored. 
          'setRNG = TRUE' sets the same state as 'R CMD check' does for
          running a package's examples.  This is currently equivalent
          to 'setRNG = {RNGkind("default", "default"); set.seed(1)}'.

prompt.echo: character; gives the prompt to be used if 'echo = TRUE'.

_D_e_t_a_i_l_s:

     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

     '_d_o_n_t_r_u_n' encloses code that should not be run.

     '_d_o_n_t_s_h_o_w' 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.

_V_a_l_u_e:

     (the value of the last evaluated expression).

_N_o_t_e:

     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'.

_A_u_t_h_o_r(_s):

     Martin Maechler and others

_S_e_e _A_l_s_o:

     'demo'

_E_x_a_m_p_l_e_s:

     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

