Unexpected result from rlang::expr using an unquoted variable

I'm trying to work through Advanced R, and I had some code that gave me an unexpected result.

lambda <- 2
rlang::expr(x ^ !!lambda - 1)

I expected it to return x^2 - 1, however it's returning x^(2 - 1)

I agree that it is counter to my expectations.
Aside from that here are two ways to write it , for your to get your original expected output

rlang::expr((x ^ !!lambda) - 1)
rlang::expr(x ^ (!!lambda) - 1)

Thanks for the response. Makes sense.

I'm still trying to figure out why these two statements have different interpretations:

lambda <- 2

rlang::expr(x ^ !!lambda - 1)
# returns: x^(2 - 1)

rlang::expr(x ^ (!!lambda) - 1)
# returns: x^2 - 1

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.