parse
Parse Expressions

Description

Returns an object of class "expression" that is the parsed version of the input.

Usage

parse(file = "", n = NULL, text = NULL, prompt = "?",
      keep.source = getOption("keep.source"),
      srcfile = NULL, encoding = "unknown") 
str2expression(text)
str2lang(s)

Arguments

file the file or connection object from which input to the parser should be read.
n the number of expressions to parse. If this number is negative, then parsing continues to the end of the file. By default, parse reads to the end of the file or text.
text an object coerced to a character vector to use as input to the parser.
prompt any object, as the prompt for standard input. In Spotfire Enterprise Runtime for R, this argument is not implemented, and a warning is generated if it is specified.
keep.source a logical value. If TRUE, the file location information is stored with parsed expressions. This information is used to print the source code for functions with the original formatting.
srcfile an environment of class "srcfile" or NULL (the default). If not NULL, this is typically an object created by utils::srcfilecopy or utils::srcfile. The integers in the output expression's srcref attribute refer to positions in srcfile$line, and the file name sometimes given in error messages is taken from srcfile$filename. If keep.source is TRUE, then a default srcfile environment is attached to the output of parse. If you supply your own value for this argument, the value should closely resemble the default one or error messages can be garbled.
encoding the string encoding to use for newly-created strings. (See Encoding.) In Spotfire Enterprise Runtime for R, this argument is not implemented.
s a single character string. str2lang uses s where parse and str2expression use text.

Details

Value

parse returns an object of mode expression containing the parsed expression(s) read from its file or text argument. If keep.source is TRUE, then the input source of the returned object has the attributes srcref and srcfile, which describe the formatting (spaces, line feeds, and comments).
str2expression(text) returns the same as parse(text=text, keep.sourc=FALSE): an expression with no attributes describing the formatting.
str2lang(s) returns an object that is the sole element of the length-one expression object that is returned by str2expression(s). This object is usually a language object, but it can be a literal value. An error is given if the length of the return value of str2expression is not one.
References
Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software.
See Also
eval, options, srcfile, scan, Encoding.
Examples
my.file <- tempfile("my.file")
cat("x <- 1\n y <- 2; max(x,y)\n", file = my.file)

# parse everything in my.file parse(file = my.file)

# parse the first expression in my.file parse(n=1, file = my.file) unlink(my.file) # clean up

# parse from a text string parse(text = "x <- 1\n y <- 2; max(x,y)") str2expression("x <- 1\n y <- 2; max(x,y)") str2lang("max(x,y)")

Package base version 6.1.4-13
Package Index