One thing that is sure to get lost in my long note on macros in R
is just how concise and powerful macros are. The problem is macros are concise, but they do a lot for you. So you get bogged down when you explain the joke.
Let’s try to be concise.
Below is an extension of an example taken from the Programming with dplyr
note.
First let’s load the package and define our symbols that hold names of columns we wish to work with later.
1 |
|
Now let’s use rlang
to substitute those symbols into a non-trivial dplyr
pipeline.
1 |
|
1 |
|
The above is very useful, we have just gotten programmatic control of all the symbol names in a pipeline. This is what is needed to wrap such a pipeline in a function and make it parametric re-usable.
The thing is Thomas Lumley’s base::bquote()
could achieve this in 2003, and Gregory R. Warnes’ gtools::strmacro()
could further automate specifying the automation in 2005.
Lets show that. First we use gtools
to build a “bquote()
wrapping factory.”
1 |
|
Now we use it to wrap some dplyr
methods (ignoring non ...
options).
1 |
|
At this point we have re-adapted 4 dplyr
methods to use bquote()
quasiquotation. This is what we mean by strmacro()
is a tool to build tools.
And here is the same pipeline again, entirely driven by bquote()
.
1 |
|
1 |
|
Like this:
Like Loading…
Related