Get startedGet started for free

Bang-bang!!

1. Bang-bang!!

Let's dig into what is going on behind the scenes of the curly-curly operator to better understand the rlang framework for tidy evaluation.

2. Unpacking curly-curly

Recall how we used the curly-curly operator previously with group_by. Since continent is not an object, but rather the name of a column in a tibble, we need to trick R a bit to be able to work with it as an unquoted function argument. Curly-curly does two things to make this work. It one - forces the function argument and then two - immediately defuses it. When a function argument is defused, R doesn't return its value like it normally would, but it returns the R expression describing how to make that value. When curly-curly sees the column name passed in, instead of sending an error that continent does not exist as an object, it forces continent through and turns it into something like a recipe. The steps are then essentially a recipe to follow that is forced to be cooked to produce the desired results. We'll further unpack this with some additional rlang syntax.

3. Bang-bang-enquo forces and defuses

Bang-bang-enquo works the same as curly-curly. We've replaced the curly-curly in the group_by call with exclamation point, exclamation point, enquo. The two exclamation points are pronounced "bang-bang". Bang-bang is called a forcing operator. The second piece here is the enquo function. It is a defusing operator. When these two are combined together in the form of bang-bang-enquo, unquoted column names in a tibble are allowed to be passed in as an argument to a function. The function then bundles up the recipe, and when the recipe is accessed in the function, it cooks the recipe to make the function work as expected on the column. We'll treat bang-bang-enquo as working together in this lesson and then see how they can be pulled apart in the next lesson. Bang-bang-enquo is performing the first and second stages of what curly-curly is doing behind the scenes, as seen by the same output here.

4. Multiple function arguments

Let's add some more generalization to our function by specifying a column to take the mean of, in addition to specifying the group_by column. First, here's the function we used to get the mean government revenue as a percentage of GDP by a specified grouping variable. Now we create a new function called grouped_mean_for_column with two arguments: one for the grouping column and another for the column to summarize col_to_mean. The next two lines of our function remain the same, again using bang-bang-enquo on group_col. Next, we summarize, and since we aren't calculating mean governmental revenue, we remove the name of the newly created summarized variable before the equals sign in summarize. We now perform a similar replacement changing the mean of gov_revenue_as_perc_gdp with the mean of bang-bang-enquo of col_to_mean.

5. Calling grouped_mean_for_column()

The perc_cvd_crd_70 column refers to what percentage of deaths in people between the ages of 30 and 70 were from cardiovascular disease (CVD), chronic respiratory disease (CRD), cancer, or diabetes. Let's see how these rates change on average across the continents for the years in our data using our grouped_mean_for_column function. We can group_by continent and then pass this mortality rate column into our col_to_mean argument. Oceania has the lowest average mortality rate, followed by the Americas and Europe.

6. Let's practice!

Try out using bang-bang with enquo in the exercises!