rlang 0.4.0
introduced the curly-curly {{ operator to simplify writing functions around tidyverse pipelines. The minor update 0.4.3 of rlang makes it possible to use { and {{ to create result names in tidyverse verbs taking pairs of names and expressions.
Install the latest version of rlang to make the new feature globally available throughout the tidyverse:
|
|
Tunnelling data-variables with curly-curly#
With the {{ operator you can tunnel data-variables (i.e. columns from the data frames) through arg-variables (function arguments):
|
|
The tunnel makes it possible to supply variables from the data frame to your wrapper function:
|
|
Without a tunnel, the ambiguity between data-variables and arg-variables causes R to complain about objects not found:
|
|
That’s because of the ambiguity between the function argument by and the data-variable Species. R has no way of knowing that you meant the variable from the data frame.
Custom result names#
In the example above, the result name is hard-coded to avg. This is an informative generic name, but returning a more specific name that reflects the context might make the function more helpful. For this reason, tidy eval functions taking dots (like dplyr::mutate(), dplyr::group_by(), or dplyr::summarise()) now support glue strings as result names.
Glue strings are implemented in the glue package . They are a flexible way of composing a string from components, interpolating R code within the string:
|
|
You can now use glue strings in result names. Note that for technical reasons you need the Walrus operator := instead of the usual =.
|
|
In addition to normal glue interpolation with {, you can also tunnel data-variables through function arguments with {{ inside the string:
|
|
And you can combine both forms of interpolation in a same glue string:
|
|
You can learn more about tunnelling variables in this RStudio::conf 2020 talk .
Acknowledgements#
Read about other bugfixes and features from the 0.4.3 release in the changelog . Many thanks to all the contributors for this release!
@chendaniely , @clauswilke , @DavisVaughan , @enoshliang , @hadley , @ianmcook , @jennybc , @krlmlr , @lionel- , @moodymudskipper , @neelan29 , @nick-youngblut , @nteetor , @romainfrancois , @TylerGrantSmith , @vspinu , and @yutannihilation

