The R Project SVN R

Rev

Rev 71394 | 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/base/man/sys.source.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
74761 kalibera 3
% Copyright 1995-2018 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
7210 hornik 6
\name{sys.source}
56186 murdoch 7
\alias{sys.source}
7210 hornik 8
\title{Parse and Evaluate Expressions from a File}
9
\description{
10
  Parses expressions in the given file, and then successively evaluates
17220 hornik 11
  them in the specified environment.
7210 hornik 12
}
8992 maechler 13
\usage{
35450 murdoch 14
sys.source(file, envir = baseenv(), chdir = FALSE,
71394 luke 15
           keep.source = getOption("keep.source.pkgs"),
74761 kalibera 16
           keep.parse.data = getOption("keep.parse.data.pkgs"),
71394 luke 17
           toplevel.env = as.environment(envir))
8992 maechler 18
}
7210 hornik 19
\arguments{
20
  \item{file}{a character string naming the file to be read from}
21
  \item{envir}{an \R object specifying the environment in which the
17220 hornik 22
    expressions are to be evaluated.  May also be a list or an integer.
23
    The default value \code{NULL} corresponds to evaluation in the base
24
    environment.  This is probably not what you want; you should
25
    typically supply an explicit \code{envir} argument.}
8992 maechler 26
  \item{chdir}{logical; if \code{TRUE}, the \R working directory is
8849 hornik 27
    changed to the directory containing \code{file} for evaluating.}
42961 ripley 28
  \item{keep.source}{logical.  If \code{TRUE}, functions keep
29
    their source including comments, see
25118 hornik 30
    \code{\link{options}(keep.source = *)} for more details.}
74761 kalibera 31
  \item{keep.parse.data}{logical.  If \code{TRUE} and \code{keep.source} is
32
    also \code{TRUE}, functions keep parse data with their source, see
33
    \code{\link{options}(keep.parse.data = *)} for more details.}
71394 luke 34
  \item{toplevel.env}{an \R environment to be used as top level while
35
    evaluating the expressions.  This argument is useful for frameworks
36
    running package tests; the default should be used in other cases}
7210 hornik 37
}
38
\details{
8992 maechler 39
  For large files, \code{keep.source = FALSE} may save quite a bit of
74761 kalibera 40
  memory. Disabling only parse data via \code{keep.parse.data = FALSE}
41
  can already save a lot.
25815 jmc 42
%%FIXME:  the following paragraph should be somewhere it's likely to be seen
43
  In order for the code being evaluated to use the correct environment
44
  (for example, in global assignments), source code in packages should
56382 murdoch 45
  call \code{\link{topenv}()}, which will return the namespace, if any,
49649 ripley 46
  the environment set up by \code{sys.source}, or the global environment
25815 jmc 47
  if a saved image is being used.
7210 hornik 48
}
8992 maechler 49
\seealso{\code{\link{source}}, and \code{\link{library}} which uses
50
  \code{sys.source}.
7210 hornik 51
}
40411 ripley 52
\examples{
44236 ripley 53
## a simple way to put some objects in an environment
54
## high on the search path
40411 ripley 55
tmp <- tempfile()
56
writeLines("aaa <- pi", tmp)
57
env <- attach(NULL, name = "myenv")
58
sys.source(tmp, env)
59
unlink(tmp)
60
search()
61
aaa
62
detach("myenv")
63
}
7210 hornik 64
\keyword{file}
65
\keyword{utilities}