The R Project SVN R

Rev

Rev 81843 | 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
81843 maechler 3
% Copyright 1995-2022 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{
85065 smeyer 20
  \item{file}{a character string naming the file to be read from.}
7210 hornik 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.
81843 maechler 23
    The default \code{\link{baseenv}()} corresponds to evaluation in the base
17220 hornik 24
    environment.  This is probably not what you want; you should
81843 maechler 25
    typically supply an explicit \code{envir} argument, see the
26
    \sQuote{Note}.}
8992 maechler 27
  \item{chdir}{logical; if \code{TRUE}, the \R working directory is
8849 hornik 28
    changed to the directory containing \code{file} for evaluating.}
42961 ripley 29
  \item{keep.source}{logical.  If \code{TRUE}, functions keep
30
    their source including comments, see
25118 hornik 31
    \code{\link{options}(keep.source = *)} for more details.}
74761 kalibera 32
  \item{keep.parse.data}{logical.  If \code{TRUE} and \code{keep.source} is
33
    also \code{TRUE}, functions keep parse data with their source, see
34
    \code{\link{options}(keep.parse.data = *)} for more details.}
71394 luke 35
  \item{toplevel.env}{an \R environment to be used as top level while
36
    evaluating the expressions.  This argument is useful for frameworks
85065 smeyer 37
    running package tests; the default should be used in other cases.}
7210 hornik 38
}
39
\details{
8992 maechler 40
  For large files, \code{keep.source = FALSE} may save quite a bit of
74761 kalibera 41
  memory. Disabling only parse data via \code{keep.parse.data = FALSE}
42
  can already save a lot.
81843 maechler 43
}
44
\section{Note on \code{envir}}{
25815 jmc 45
  In order for the code being evaluated to use the correct environment
46
  (for example, in global assignments), source code in packages should
56382 murdoch 47
  call \code{\link{topenv}()}, which will return the namespace, if any,
49649 ripley 48
  the environment set up by \code{sys.source}, or the global environment
25815 jmc 49
  if a saved image is being used.
7210 hornik 50
}
81843 maechler 51
\seealso{\code{\link{source}}, and \code{\link{loadNamespace}} which
52
  is called from \code{\link{library}(.)} and uses \code{sys.source(.)}.
7210 hornik 53
}
40411 ripley 54
\examples{
44236 ripley 55
## a simple way to put some objects in an environment
56
## high on the search path
40411 ripley 57
tmp <- tempfile()
58
writeLines("aaa <- pi", tmp)
59
env <- attach(NULL, name = "myenv")
60
sys.source(tmp, env)
61
unlink(tmp)
62
search()
63
aaa
64
detach("myenv")
65
}
7210 hornik 66
\keyword{file}
67
\keyword{utilities}