Using the rlang package, I wonder what is the difference between sym() and parse_expr(). Consider for example the following expressions:
ex1 = sym('a')
ex2 = parse_expr('a')
They both return
a
identical(ex1, ex2)
[1] TRUE
Suppose now I need a quosure:
ex3 = quo(!!sym('a'))
ex4 = quo(!!parse_expr('a'))
In both case, the result is:
<quosure>
expr: ^a
env: global
identical(ex3, ex4)
[1] TRUE
However, the two following are not the same for some reasons.
ex5 = quo(!!sym('a - b'))
ex6 = quo(!!parse_expr('a - b'))
Apparently they are identical as both return:
<quosure>
expr: ^a - b
env: global
Yet,
identical(ex5, ex6)
[1] FALSE
Some my question is what are the differences between sym() and parse_expr()? What does one do that the other cannot? And why ex5 is apparently similar to ex6 but identical (ex5, ex6) returns FALSE?
Referencing my answer to this question:
A symbol is a way to refer to an R object, basically the "name" of an object. So
sym
is similar toas.name
in base R.parse_expr
on the other hand transforms some text into R expressions. This is similar toparse
in base R.Expressions can be any R code, not just code that references R objects. So you can parse the code that references an R object, but you can't turn some random code into
sym
if the object that the code references does not exist.In general, you will use
sym
when your string refers to an object (althoughparse_expr
would also work), and useparse_expr
when you are trying to parse any other R code for further evaluation.For your first example,
a
can be both a name that references an object AND an expression, so turning it into either asym
orparse_expr
would practically mean the same thing.For your last example however,
a - b
is really intended to be an expression (unless you have an R object that is weirdly nameda - b
). By printing the following, you will see that usingsym
vsparse_expr
for R code that is intended to be an expression, not an R object produces two different results:Here,
sym
turnsa - b
into a name/symbol of an object, hence the back ticks arounda - b
, whileparse_expr
turns it into an expression as expected.To augment the previous answer, note that
ex5
andex6
are not in fact identical.