[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  parse: Parse R Expressions

##  Description

` parse() ` returns the parsed but unevaluated expressions in an ` [
expression ](/link/expression?package=base&version=3.6.2) ` , a “list” of ` [
call ](/link/call?package=base&version=3.6.2) ` s.

` str2expression(s) ` and ` str2lang(s) ` return special versions of `
parse(text=s, keep.source=FALSE) ` and can therefore be regarded as
transforming character strings ` s ` to expressions, calls, etc.

##  Usage

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

##  Arguments

file

a [ connection ](/link/connection?package=base&version=3.6.2) , or a character
string giving the name of a file or a URL to read the expressions from. If `
file ` is ` "" ` and ` text ` is missing or ` NULL ` then input is taken from
the console.

n

integer (or coerced to integer). The maximum number of expressions to parse.
If ` n ` is ` NULL ` or negative or ` NA ` the input is parsed in its
entirety.

text

character vector. The text to parse. Elements are treated as if they were
lines of a file. Other  R  objects will be coerced to character if possible.

prompt

the prompt to print when parsing from the keyboard. ` NULL ` means to use  R
's prompt, ` getOption("prompt") ` .

keep.source

a logical value; if ` TRUE ` , keep source reference information.

srcfile

` NULL ` , a character vector, or a ` [ srcfile
](/link/srcfile?package=base&version=3.6.2) ` object. See the ‘Details’
section.

encoding

encoding to be assumed for input strings. If the value is ` "latin1" ` or `
"UTF-8" ` it is used to mark character strings as known to be in Latin-1 or
UTF-8: it is not used to re-encode the input. To do the latter, specify the
encoding as part of the connection ` con ` or _via_ ` [ options
](/link/options?package=base&version=3.6.2) (encoding=) ` : see the example
under ` [ file ](/link/file?package=base&version=3.6.2) ` .

s

a ` [ character ](/link/character?package=base&version=3.6.2) ` vector of
length ` 1 ` , i.e., a “string”.

##  Value

` parse() ` and ` str2expression() ` return an object of type ` " [ expression
](/link/expression?package=base&version=3.6.2) " ` , for ` parse() ` with up
to ` n ` elements if specified as a non-negative integer.

` str2lang(s) ` , ` s ` a string, returns “a ` [ call
](/link/call?package=base&version=3.6.2) ` or simpler”, see the ‘Details:’
section.

When ` srcfile ` is non- ` NULL ` , a ` "srcref" ` attribute will be attached
to the result containing a list of ` [ srcref
](/link/srcref?package=base&version=3.6.2) ` records corresponding to each
element, a ` "srcfile" ` attribute will be attached containing a copy of `
srcfile ` , and a ` "wholeSrcref" ` attribute will be attached containing a `
[ srcref ](/link/srcref?package=base&version=3.6.2) ` record corresponding to
all of the parsed text. Detailed parse information will be stored in the `
"srcfile" ` attribute, to be retrieved by ` [ getParseData
](/link/getParseData?package=base&version=3.6.2) ` .

A syntax error (including an incomplete expression) will throw an error.

Character strings in the result will have a declared encoding if ` encoding `
is ` "latin1" ` or ` "UTF-8" ` , or if ` text ` is supplied with every element
of known encoding in a Latin-1 or UTF-8 locale.

##  Partial parsing

When a syntax error occurs during parsing, ` parse ` signals an error. The
partial parse data will be stored in the ` srcfile ` argument if it is a ` [
srcfile ](/link/srcfile?package=base&version=3.6.2) ` object and the ` text `
argument was used to supply the text. In other cases it will be lost when the
error is triggered.

The partial pa