The R Project SVN R

Rev

Rev 61650 | Rev 66161 | 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/parse.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{parse}
56186 murdoch 7
\alias{parse}
2 r 8
\title{Parse Expressions}
24641 hornik 9
\description{
10
  \code{parse} returns the parsed but unevaluated expressions in a
40180 ripley 11
  list.
24641 hornik 12
}
2 r 13
\usage{
61650 murdoch 14
parse(file = "", n = NULL, text = NULL, prompt = "?",
15
      keep.source = getOption("keep.source"), srcfile,
40672 ripley 16
      encoding = "unknown")
2 r 17
}
18
\arguments{
54236 ripley 19
  \item{file}{a \link{connection}, or a character string giving the name of a
13983 ripley 20
    file or a URL to read the expressions from.
2381 maechler 21
    If \code{file} is \code{""} and \code{text} is missing or \code{NULL}
11656 ripley 22
    then input is taken from the console.}
40019 ripley 23
  \item{n}{integer (or coerced to integer).  The maximum number of
40017 ripley 24
    expressions to parse.  If \code{n} is \code{NULL} or negative or
25
    \code{NA} the input is parsed in its entirety.}
38728 ripley 26
  \item{text}{character vector.  The text to parse.  Elements are treated
40017 ripley 27
    as if they were lines of a file.  Other \R objects will be coerced
51330 ripley 28
    to character if possible.}
15561 ripley 29
  \item{prompt}{the prompt to print when parsing from the keyboard.
24641 hornik 30
    \code{NULL} means to use \R's prompt, \code{getOption("prompt")}.}
61650 murdoch 31
  \item{keep.source}{a logical value; if \code{TRUE}, keep 
32
    source reference information.}
61433 ripley 33
  \item{srcfile}{\code{NULL}, a character vector, or a
61399 murdoch 34
    \code{\link{srcfile}} object.  See the \sQuote{Details} section.}
46855 ripley 35
  \item{encoding}{encoding to be assumed for input strings.  If the
36
    value is \code{"latin1"} or \code{"UTF-8"} it is used to mark
37
    character strings as known to be in Latin-1 or UTF-8: it is not used
38
    to re-encode the input.  To do the latter, specify the encoding as
39
    part of the connection \code{con} or \emph{via}
40
    \code{\link{options}(encoding=)}: see the example under
46858 ripley 41
    \code{\link{file}}.}
2 r 42
}
24300 ripley 43
\references{
44
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
45
  \emph{The New S Language}.
47262 ripley 46
  Wadsworth & Brooks/Cole.
61664 murdoch 47
 
48
  Murdoch, D. (2010).  
49
  \href{http://journal.r-project.org/archive/2010-2/RJournal_2010-2_Murdoch.pdf}{Source 
50
  References}.  \emph{The R Journal} 2/2, 16-19.
24300 ripley 51
}
2 r 52
\seealso{
48 hornik 53
  \code{\link{scan}}, \code{\link{source}}, \code{\link{eval}},
54
  \code{\link{deparse}}.
61664 murdoch 55
 
56
  The source reference information can be used for debugging (see e.g.
57
  \code{\link{setBreakpoint}}) and profiling (see
58
  \code{\link{Rprof}}). It can be examined by \code{\link{getSrcref}}
59
  and related functions.  More detailed information is available through
60
  \code{\link{getParseData}}.  
2 r 61
}
12256 pd 62
\details{
40019 ripley 63
  If \code{text} has length greater than zero (after coercion) it is used in
40017 ripley 64
  preference to \code{file}.
61433 ripley 65
 
13983 ripley 66
  All versions of \R accept input from a connection with end of line
12256 pd 67
  marked by LF (as used on Unix), CRLF (as used on DOS/Windows)
47704 ripley 68
  or CR (as used on classic Mac OS).  The final line can be incomplete,
23381 ripley 69
  that is missing the final EOL marker.
21139 ripley 70
 
71
  See \code{\link{source}} for the limits on the size of functions
45921 ripley 72
  that can be parsed (by default).
38728 ripley 73
 
74
  When input is taken from the console, \code{n = NULL} is equivalent to
50623 ripley 75
  \code{n = 1}, and \code{n < 0} will read until an EOF character is
76
  read.  (The EOF character is Ctrl-Z for the Windows front-ends.)  The
77
  line-length limit is 4095 bytes when reading from the console (which
78
  may impose a lower limit: see \sQuote{An Introduction to R}).
40261 murdoch 79
 
80
  The default for \code{srcfile} is set as follows.  If
61650 murdoch 81
  \code{keep.source} is not \code{TRUE}, \code{srcfile}
61399 murdoch 82
  defaults to a character string, either \code{"<text>"} or one
61650 murdoch 83
  derived from \code{file}.  When \code{keep.source} is
61399 murdoch 84
  \code{TRUE}, if \code{text} is used, \code{srcfile} will be set to a
85
  \code{\link{srcfilecopy}} containing the text.  If a character
86
  string is used for \code{file}, a \code{\link{srcfile}} object
87
  referring to that file will be used.
61433 ripley 88
 
61399 murdoch 89
  When \code{srcfile} is a character string, error messages will
61664 murdoch 90
  include the name, but source reference information will not be added
91
  to the result.  When \code{srcfile} is a \code{\link{srcfile}}
92
  object, source reference information will be retained. 
12256 pd 93
}
38728 ripley 94
\value{
38738 ripley 95
  An object of type \code{"\link{expression}"}, with up to \code{n}
96
  elements if specified as a non-negative integer.
97
 
40261 murdoch 98
  When \code{srcfile} is non-\code{NULL}, a \code{"srcref"} attribute
61433 ripley 99
  will be attached to the result containing a list of
100
  \code{\link{srcref}} records corresponding to each element, a
51758 murdoch 101
  \code{"srcfile"} attribute will be attached containing a copy of
102
  \code{srcfile}, and a \code{"wholeSrcref"} attribute will be
103
  attached containing a \code{\link{srcref}} record corresponding to
59883 murdoch 104
  all of the parsed text. Detailed parse information will be stored in
61433 ripley 105
  the \code{"srcfile"} attribute, to be retrieved by
59883 murdoch 106
  \code{\link{getParseData}}.
61433 ripley 107
 
38738 ripley 108
  A syntax error (including an incomplete expression) will throw an error.
40696 ripley 109
 
110
  Character strings in the result will have a declared encoding if
46864 ripley 111
  \code{encoding} is \code{"latin1"} or \code{"UTF-8"}, or if
112
  \code{text} is supplied with every element of known encoding in a
113
  Latin-1 or UTF-8 locale.
38728 ripley 114
}
61664 murdoch 115
 
2 r 116
\examples{
61168 ripley 117
cat("x <- c(1, 4)\n  x ^ 3 -10 ; outer(1:7, 5:9)\n", file = "xyz.Rdmped")
48 hornik 118
# parse 3 statements from the file "xyz.Rdmped"
119
parse(file = "xyz.Rdmped", n = 3)
8102 ripley 120
unlink("xyz.Rdmped")
2 r 121
}
286 maechler 122
\keyword{file}
123
\keyword{programming}
11656 ripley 124
\keyword{connection}