source
Read Expressions from a File or a Connection
Description
Parses and then evaluates each expression in the given file.
By default, the evaluation is done in the current environment.
Usage
source(file, local = FALSE, echo = verbose, print.eval = echo,
verbose = getOption("verbose"), prompt.echo = getOption("prompt"),
max.deparse.length = 150, chdir = FALSE, encoding = getOption("encoding"),
continue.echo = getOption("continue"), skip.echo = 0,
keep.source = getOption("keep.source"))
Arguments
file |
a connection or a character string giving the name of a file or URL.
The file is parsed, and the resulting expressions are evaluated.
|
local |
a logical value or an environment; if FALSE,
the expressions will be evaluated in the top (global) frame;
if TRUE, locally to the function calling source;
if an environment, locally to that environment.
The default is FALSE.
|
echo |
a logical value. If TRUE, each expression is printed, along
with a prompt, before it is evaluated. The default is FALSE.
|
print.eval |
a logical value;
if TRUE, the result of each expression is printed.
Default value is the same as echo.
|
verbose | This argument is not yet implemented.
a logical value;
if TRUE, more diagnostics are printed when parsing and evaluation.
|
prompt.echo |
a character string, to specify the prompt to be used if echo = TRUE.
|
max.deparse.length | This argument is not yet implemented.
integer;
only used when echo is TRUE,
to specify the maximal number of characters output
for the deparse of a single expression.
|
chdir |
a logical value. If TRUE, and if file is a pathname,
the working directory is changed temporarily to the directory containing
the file to evaluate. The default is FALSE.
|
encoding |
character string.
the character encoding for the input stream,
used for converting input bytes to characters.
This can be any of the encodings accepted by iconv,
including "native.enc".
This argument is not used when the file argument is a connection,
since a connection already has an associated encoding (see file).
|
continue.echo |
a character string, to specify the continuation prompt when echo = TRUE.
|
skip.echo |
integer, to specify how many comment lines at the start of the file
to skip if echo = TRUE.
|
keep.source |
a logical value; if TRUE (the default),
file location information is stored with parsed expressions.
This is used to print the source code for functions with
the original formatting.
|
Details
If echo is FALSE, the expressions are not printed.
If you still want to echo something, you should use the print function in your script.
If options("keep.source") is TRUE, the source of
functions is kept so they can be listed exactly as input.
Value
returns the value of the last evaluated expression in the file.
See Also
Examples
fl <- tempfile()
cat("xx<-function(x)x+1\n", file=fl)
cat("print(1:10)\n", file=fl, append=TRUE)
source(fl)
# defines the function xx in the global environment,
# and prints
# [1] 1 2 3 4 5 6 7 8 9 10
source(fl, echo=TRUE)
# also prints each expression in the file
unlink(fl)