The R Project SVN R

Rev

Rev 59759 | Rev 66444 | 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/source.Rd
2
% Part of the R package, http://www.R-project.org
59759 ripley 3
% Copyright 1995-2012 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
57939 hornik 6
\newcommand{\CRANpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
7
 
2 r 8
\name{source}
20889 ripley 9
\title{Read R Code from a File or a Connection}
53899 ripley 10
\description{
11
  \code{source} causes \R to accept its input from the named file or URL
12
  or connection.  Input is read and \code{\link{parse}}d from that file
13
  until the end of the file is reached, then the parsed expressions are
14
  evaluated sequentially in the chosen environment.
15
}
2 r 16
\usage{
2394 maechler 17
source(file, local = FALSE, echo = verbose, print.eval = echo,
30912 ripley 18
       verbose = getOption("verbose"),
19
       prompt.echo = getOption("prompt"),
32569 ripley 20
       max.deparse.length = 150, chdir = FALSE,
39996 murdoch 21
       encoding = getOption("encoding"),
22
       continue.echo = getOption("continue"),
46480 ripley 23
       skip.echo = 0, keep.source = getOption("keep.source"))
2 r 24
}
56186 murdoch 25
\alias{source}
2 r 26
\arguments{
54236 ripley 27
  \item{file}{a \link{connection} or a character string giving the pathname
46855 ripley 28
    of the file or URL to read from.  \code{""} indicates the connection
29
    \code{\link{stdin}()}.}
56102 ripley 30
  \item{local}{\code{TRUE}, \code{FALSE} or an environment, determining
59274 ripley 31
    where the parsed expressions are evaluated.  \code{FALSE} (the
32
    default) corresponds to the user's workspace (the global
33
    environment) and \code{TRUE} to the environment from which
34
    \code{source} is called.}
11656 ripley 35
  \item{echo}{logical; if \code{TRUE}, each expression is printed
36
    after parsing, before evaluation.}
37
  \item{print.eval}{logical; if \code{TRUE}, the result of
38
    \code{eval(i)} is printed for each expression \code{i}; defaults
46727 ripley 39
    to the value of \code{echo}.}
11656 ripley 40
  \item{verbose}{if \code{TRUE}, more diagnostics (than just
41
    \code{echo = TRUE}) are printed during parsing and evaluation of
42
    input, including extra info for \bold{each} expression.}
43
  \item{prompt.echo}{character; gives the prompt to be used if
44
    \code{echo = TRUE}.}
45
  \item{max.deparse.length}{integer; is used only if \code{echo} is
43435 ripley 46
    \code{TRUE} and gives the maximal number of characters output for
47
    the deparse of a single expression.}
32492 ripley 48
  \item{chdir}{logical; if \code{TRUE} and \code{file} is a pathname,
49
    the \R working directory is temporarily changed to the directory
50
    containing \code{file} for evaluating.}
46850 ripley 51
  \item{encoding}{character vector.  The encoding(s) to be assumed when
35640 ripley 52
    \code{file} is a character string: see \code{\link{file}}.  A
53897 ripley 53
    possible value is \code{"unknown"} when the encoding is guessed: see
53899 ripley 54
    the \sQuote{Encodings} section.}
39996 murdoch 55
  \item{continue.echo}{character; gives the prompt to use on
56
    continuation lines if \code{echo = TRUE}.}
57
  \item{skip.echo}{integer; how many comment lines at the start of the
58
    file to skip if \code{echo = TRUE}.}
46480 ripley 59
  \item{keep.source}{logical: should the source formatting be retained
56321 ripley 60
    when echoing expressions, if possible?}
2 r 61
}
12256 pd 62
\details{
46727 ripley 63
  Note that running code via \code{source} differs in a few respects
64
  from entering it at the \R command line.  Since expressions are not
65
  executed at the top level, auto-printing is not done.  So you will
66
  need to include explicit \code{print} calls for things you want to be
57939 hornik 67
  printed (and remember that this includes plotting by \CRANpkg{lattice},
46727 ripley 68
  FAQ Q7.22).  Since the complete file is parsed before any of it is
55555 ripley 69
  run, syntax errors result in none of the code being run.  If an error
46727 ripley 70
  occurs in running a syntactically correct script, anything assigned
71
  into the workspace by code that has been run will be kept (just as
72
  from the command line), but diagnostic information such as
73
  \code{\link{traceback}()} will contain additional calls to
58773 ripley 74
  \code{\link{withVisible}}.
46727 ripley 75
 
20889 ripley 76
  All versions of \R accept input from a connection with end of line
50137 ripley 77
  marked by LF (as used on Unix), CRLF (as used on DOS/Windows) or CR
78
  (as used on classic Mac OS) and map this to newline.  The final line
79
  can be incomplete, that is missing the final end-of-line marker.
20889 ripley 80
 
54234 murdoch 81
  If \code{keep.source} is true (the default in interactive use), the
82
  source of functions is kept so they can be listed exactly as input.
61433 ripley 83
 
56410 ripley 84
%   Using \code{echo = TRUE} and \code{keep.source = TRUE} may interact
85
%   badly with source code that includes \samp{#line nn "filename"}
86
%   directives (e.g. code produced by older versions of
87
%   \code{\link{Stangle}()}): \code{source()} will attempt to obtain the
88
%   source from the named file which may have changed since the code was
89
%   produced.  Use \code{keep.source = FALSE} to avoid this.
61433 ripley 90
 
38967 ripley 91
  Unlike input from a console, lines in the file or on a connection can
45921 ripley 92
  contain an unlimited number of characters.
61433 ripley 93
 
39996 murdoch 94
  When \code{skip.echo > 0}, that many comment lines at the start of
95
  the file will not be echoed.  This does not affect the execution of
96
  the code at all.  If there are executable lines within the first
97
  \code{skip.echo} lines, echoing will start with the first of them.
43435 ripley 98
 
99
  If \code{echo} is true and a deparsed expression exceeds
100
  \code{max.deparse.length}, that many characters are output followed by
61433 ripley 101
  \code{ .... [TRUNCATED] }.
12256 pd 102
}
53899 ripley 103
\section{Encodings}{
104
  By default the input is read and parsed in  the current encoding of
105
  the \R session.  This is usually what it required, but occasionally
106
  re-encoding is needed, e.g. if a file from a UTF-8-using system is to
107
  be read on Windows (or \emph{vice versa}).
61433 ripley 108
 
53899 ripley 109
  The rest of this paragraph applies if \code{file} is an actual
110
  filename or URL (and not \code{""} nor a connection).  If
111
  \code{encoding = "unknown"}, an attempt is made to guess the encoding:
112
  the result of \code{\link{localeToCharset}()} is used as a guide.  If
113
  \code{encoding} has two or more elements, they are tried in turn until
114
  the file/URL can be read without error in the trial encoding.  If an
115
  actual \code{encoding} is specified (rather than the default or
116
  \code{"unknown"}) in a Latin-1 or UTF-8 locale then character strings
117
  in the result will be translated to the current encoding and marked as
118
  such (see \code{\link{Encoding}}).
61433 ripley 119
 
53899 ripley 120
  If \code{file} is a connection (including one specified by \code{""},
121
  it is not possible to re-encode the input inside \code{source}, and so
122
  the \code{encoding} argument is just used to mark character strings in the
123
  parsed input in Latin-1 and UTF-8 locales: see \code{\link{parse}}.
124
}
24300 ripley 125
\references{
126
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
127
  \emph{The New S Language}.
47262 ripley 128
  Wadsworth & Brooks/Cole.
24300 ripley 129
}
16 maechler 130
\seealso{
30461 ripley 131
  \code{\link{demo}} which uses \code{source};
20889 ripley 132
  \code{\link{eval}}, \code{\link{parse}} and \code{\link{scan}};
133
  \code{\link{options}("keep.source")}.
40411 ripley 134
 
135
  \code{\link{sys.source}} which is a streamlined version to source a
136
  file into an environment.
61433 ripley 137
 
54234 murdoch 138
  \sQuote{The R Language Definition} for a discussion of source
61433 ripley 139
  directives.
16 maechler 140
}
37100 maechler 141
\examples{
142
## If you want to source() a bunch of files, something like
143
## the following may be useful:
144
 sourceDir <- function(path, trace = TRUE, ...) {
59759 ripley 145
    for (nm in list.files(path, pattern = "[.][RrSsQq]$")) {
61433 ripley 146
       if(trace) cat(nm,":")
37100 maechler 147
       source(file.path(path, nm), ...)
148
       if(trace) cat("\n")
149
    }
150
 }
151
}
16 maechler 152
\keyword{file}
286 maechler 153
\keyword{programming}
11656 ripley 154
\keyword{connection}