1

I'm reading through some notes on quasiquotation here: https://dplyr.tidyverse.org/articles/programming.html.

After my first read, I've tried a few things out. One in particular has me confused:

x <- "foo"
q <- quo(x)

print(x)
<quosure>
expr: ^x
env:  global

Great, I've created a quosure! Then I figure

!!q

will immediately evaluate the expression, producing "foo". Though naturally, that is wrong!

!!q
Error in !q : invalid argument type

I don't understand why. What am I missing?


Running: R version 3.5.1 (2018-07-02) -- "Feather Spray" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit)

package rlang version: 0.2.1

Al R.
  • 2,430
  • 4
  • 28
  • 40
  • By the way I'm working on a tidyeval bookdown. WIP but it should be already better than the dplyr vignette https://tidyeval.tidyverse.org/ – Lionel Henry Sep 12 '18 at 05:09
  • Looking forward to it! – Al R. Sep 12 '18 at 12:57
  • 1
    FWIW, I found these bits helpful: "Identify where these variables are passed to other quoting functions and unquote with !! ... We end up with a function that automatically quotes its arguments group_var and summary_var and unquotes them when they are passed to other quoting functions." Making the connection that dplyr functions ARE quoting functions was helpful. Also, the error message provided by UQ() was much more informative than that provided by !! -- you don't just use !! in open code. – Al R. Sep 12 '18 at 13:04
  • Yeah, that's the biggest downside of the tidyeval UI, `!!` is not a real operator so it won't necessarily be an error when you use it outside a tidyeval function, it will try to take the double negation of its argument. – Lionel Henry Sep 14 '18 at 10:32

1 Answers1

2

I tested your method and you're partially correct.

You can use !!q, but only in a quasiquote environment.

> !! q
  Error in !q : invalid argument type
> UQ(q)
  Error: `UQ()` can only be used within a quasiquoted argument
> quo(!! q)
  <quosure>
  expr: ^x
  env:  global
> quo(!!q)
  <quosure>
  expr: ^x
  env:  global

I've used both UQ and !! to make sure my answer works consistently

MattMyint
  • 66
  • 7