These functions parse and transform text into R expressions. This
is the first step to interpret or evaluate a piece of R code
written by a programmer.
parse_expr()
returns one expression. If the text contains more
than one expression (separated by semicolons or new lines), an
error is issued. On the other hand parse_exprs()
can handle
multiple expressions. It always returns a list of expressions
(compare to base::parse()
which returns a base::expression
vector). All functions also support R connections.
parse_expr()
concatenates x
with \\n
separators prior to
parsing in order to support the roundtrip
parse_expr(expr_deparse(x))
(deparsed expressions might be
multiline). On the other hand, parse_exprs()
doesn't do any
concatenation because it's designed to support named inputs. The
names are matched to the expressions in the output, which is
useful when a single named string creates multiple expressions.
In other words, parse_expr()
supports vector of lines whereas
parse_exprs()
expects vectors of complete deparsed expressions.
parse_quo()
and parse_quos()
are variants that create a
quosure. Supply env = current_env()
if you're parsing
code to be evaluated in your current context. Supply env = global_env()
when you're parsing external user input to be
evaluated in user context.
Unlike quosures created with enquo()
, enquos()
, or {{
, a
parsed quosure never contains injected quosures. It is thus safe
to evaluate them with eval()
instead of eval_tidy()
, though
the latter is more convenient as you don't need to extract expr
and env
.