Chapter 19 Quasiquotation
19.2 Motivation
How does cement
compare to glue
? Can we look at the source code for quasiquotation and using {}
for interpolation?
They are opposite; while glue
evaluates expressions inside a string, cement
quotes expressions.
19.3.4 Substitution
Is it supplied by the developer of the code or the user of the code? In other words, is it fixed (supplied in the body of the function) or varying (supplied via an argument)?
What are two examples (from the chapter or our own) of the former vs latter
This destinction can be put as: do you want exactly what the developer/coder typed or do you want what was put into the function? This will tell you if you want to use expr
or enexpr
Hadley creates a table for quoting expressions using base R and tidy eval, but there isn’t really a one to one relationship here. For instance substitute
is compared to enexprs
but that fails here?
That is because they are not a 1-1 relationship, substitute
has 5
different functions under the hood (but they are not S3
methods!!), these are only related somewhat.
Exercises 19.3.6.5
What does the argument .named
do in exprs
? What does “to ensure all dots are named” mean?
You can pass extra arguments to your function as dots, and this will ensure that those extra arguments are named
19.4.6 Polite fiction of !!
What exactly is happening here? Why is y
evaluating to 6
?!
## [1] 2 3 4 5 6
Any number that isn’t 0
is TRUE
in R:
## [1] TRUE
So because y==TRUE
, with
is taking every element in x
and adding 1
to it because once you introduce a +
inside with
you’re essentially saying as.numeric(TRUE)
which is 1
.
## [1] TRUE TRUE TRUE TRUE TRUE
!x
essentially really means !as.logical(x)
, 1:5 + x
essentially really means 1:5 + as.integer(x)
, so we end up with 1:5 + as.integer(!(!(as.logical(y))))
19.4.7 Nonstandard ASTs
When talking about non-standard ASTs Hadley says:
These are valid, and occasionally useful, but their correct use is beyond the scope of this book.
What is their correct use?
A use for this could potentially be if you need to manipulate an AST and subsitute an environment within the AST? In the question below we are creating a non-standard AST (not that we should be!)
19.4.8.2 Exercises
What exactly is going on here? What is <inline integer>
?
## █─mean
## └─█─`:`
## ├─1
## └─10
## █─mean
## └─<inline integer>
The inline integer is the actual integers being inserted into the AST rather than as expressions
Non-quoting
What exactly is the difference between “turn quoting off” and “using unquoting” – maybe to explain this we can we come up with an example for “turning quoting off” when we expect “unquoting” and therefore the operation fails?
In base R functions it’s all or nothing: you can either quote everything or nothing, you cannot selectively quote expressions.
19.6 (…)
Based on this thread it looks like ...
should be the first line of defense before using tidyeval - why?
The first option lets the robust function group_by
figure out any quoting. Furthermore, the second option only allows the user to supply a single column to group by and assumes the user is passing in a valid column.
Can we go over the “spatting” example, I have no idea what this means.
I think “splatting” is the correct term*. It means taking a single argument in a list and interpreting it as separate arguments. Hadley is referring to the specific use of !!!
to unpack a list into function arguments.
- at least googling for “splatting Ruby” gives lots of relevant hits vs “spatting Ruby”
19.6.2 exec()
Can we use this function instead of do.call
in our Expressions shiny app? Do we need to change argumentlist()
to use list2
rather than list
?
19.6.3 dots_list()
This function seems pretty rad, what’s a use case for it in the wild?
We can use this function anytime there’s a use for list2
!
19.6.4 With base R
What is going on with this RCurl example and how does it avoid using do.call
? Can we find some Hadley source code that uses it since he said he liked it at one point? Why do you think he moved away from it?
The dots
object combines all the arguments passed as dots and the arguments not passed as dots. This gives a lot (perhaps too much) leeway to the user of the function.