0

I have two variables, a and b. I try to compute a-b using expression with the rlang package. quo(a-b) returns, as expected:

<quosure: global>
~a - b

However, I have a and b as strings. So I tried: quo(!!sym("a-b")), which results in

<quosure: global>
~`a-b` #(note the '')

So the question is why I obtain ~'a-b', and not ~a-b. How can I simply obtain ~a-b. Note that quo(!!sym("a")) returns, as expected:

<quosure: global>
~a

So it seems that there is an issue with the - sign (same will occur with *). Is it related to some special characters / non standard evaluation issues? How can I solve that?

Rtist
  • 3,825
  • 2
  • 31
  • 40

1 Answers1

1

Use parse_expr...

library(rlang)
q0 <- quo(a - b)
q1 <- quo(!!parse_expr("a - b"))
identical(q0, q1)
# [1] TRUE

...or parse_quo:

q2 <- parse_quo("a - b", global_env())
identical(q0, q2)
# [1] TRUE

See the discussion here: https://github.com/r-lib/rlang/issues/116

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
  • Thanks. Can you explain what is the difference between the sym() and parse_exp()? It is unclear from the discussion mentioned. – Rtist May 03 '18 at 13:53
  • You should ask it as a new question, so other people who have the same question can find it when they search for it. But in short, `sym("a-b")` creates one symbol `\`a-b\``, in the sense of `\`a-b\` <- 5`. – Weihuang Wong May 03 '18 at 14:06