6 Modifying inputs

With the quote-and-unquote pattern, quoted arguments are passed to other functions as is. In many cases you’ll find this to be too restrictive. This chapter will guide you through the steps required to pass custom argument names and custom quoted expressions.

6.1 Modifying names

When your function creates new columns in a data frame it’s often a good idea to give them names that reflect the meaning of those columns. In this section you’ll learn how to:

  • Create default names for quoted arguments.
  • Unquote names.

6.1.1 Default argument names

If you are familiar with dplyr you have probably noticed that new columns are given default names when you don’t supply one explictly to mutate() or summarise(). These default names are not practical for further manipulation but they are helpful to remind rushed users what their new column is about:

You can create default names by applying as_label() to any expressions:

Including automatically quoted arguments:

Lists of quoted expressions require a different approach because we don’t want to override user-supplied names. The easiest way is to call enquos() with .named = TRUE. With this option, all unnamed arguments get a default name:

6.1.2 Unquoting argument names

Argument names are one of the most common occurrence of quotation in R. There is no fundamental difference between these two ways of creating a "myname" string:

Where there is quotation it is natural to have unquotation. For this reason, tidy eval makes it possible to use !! to unquote names. Unfortunately we’ll have to use a somewhat peculiar syntax to unquote names because using complex expressions on the left-hand side of = is not valid R code:

Instead you’ll have to unquote of the LHS of :=. This vestigial operator is interpreted by tidy eval functions in exactly the same way as = but with !! support:

Another way of achieving the same result is to splice a named list of arguments:

This works because !!! uses the names of the list as argument names. This is a great pattern when you are dealing with multiple arguments:

6.1.3 Prefixing quoted arguments

Now that we know how to unquote argument, let’s apply informative prefixes to the names of the columns created in grouped_mean(). We’ll start with the summary variable:

  1. Get the default name of the quoted summary variable.
  2. Prepend it with a prefix.
  3. Unquote it with !! and :=.

Regarding the grouping variables, this is a case where explictly quoting and unquoting ... pays off because we need to change the names of the list of quoted dots:

  • Give default names to quoted dots with .named = TRUE.
  • Prepend the names of the list with a prefix.
  • Unquote-splice the list of quoted arguments as usual.

6.2 Modifying quoted expressions

The quote-and-unquote pattern is a powerful and versatile technique. In this section we’ll use it for modifying quoted arguments.

In dealing with multiple arguments, we have created a version of grouped_mean() that takes multiple grouping variables. Say we would like to take multiple summary variables instead. We could start by replacing summary_var with the ... argument:

The quoting part is easy. But how do we go about taking the average of each argument before passing them on to summarise()? We’ll have to modify the list of summary variables.

6.2.1 Expanding quoted expressions with expr()

Quoting and unquoting is an effective technique for modifying quoted expressions. But we’ll need to add one more function to our toolbox to work around the lack of unquoting support in quote().

As we saw, the fundamental quoting function in R is quote(). All it does is return its quoted argument:

quote() does not support quasiquotation but tidy eval provides a variant that does. With expr(), you can quote expressions with full unquoting support:

Note what just happened: by quoting-and-unquoting, we have expanded existing quoted expressions! This is the key to modifying expressions before passing them on to other quoting functions. For instance we could loop over the summary variables and unquote each of them in a mean() expression:

Let’s fix grouped_mean3() using this pattern: