Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do {{}} double curly brackets work in dplyr?

I saw Hadley's talk at RConf and he mentioned using double brackets for calling variables in tidy evals.

I searched Google but I couldn't find anything talking about when to use them.

What's the use case for double brackets in dplyr?

like image 770
Cauder Avatar asked Sep 25 '20 13:09

Cauder


People also ask

What do curly brackets do in R?

The curly brackets are used to denote a block of code in a function. So, say we need a function to calculate the standard error we might do this. The square brackets are used to subset vectors and data frames.

What are {} used for?

Brackets are used to insert explanations, corrections, clarifications, or comments into quoted material. Brackets are always used in pairs; you must have both an opening and a closing bracket.

How do you use double curly braces in HTML?

Declare a template in the HTML file. Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.

What does curly brackets mean in shell script?

The end of the variable name is usually signified by a space or newline. But what if we don't want a space or newline after printing the variable value? The curly braces tell the shell interpreter where the end of the variable name is.


1 Answers

{{}} (curly-curly) have lot of applications. It is called as meta-programming and is used for writing functions. For example, consider this example :

library(dplyr)
library(rlang)

mtcars %>% group_by(cyl) %>% summarise(new_mpg = mean(mpg))

# A tibble: 3 x 2
#    cyl new_mpg
#  <dbl>   <dbl>
#1     4    26.7
#2     6    19.7
#3     8    15.1

Now if you want to write this as a function passing unquoted variables (not a string), you can use {{}} as :

my_fun <- function(data, group_col, col, new_col) {
  data %>%
    group_by({{group_col}}) %>%
    summarise({{new_col}} := mean({{col}}))
}

mtcars %>% my_fun(cyl, mpg, new_mpg)

#    cyl new_mpg
#  <dbl>   <dbl>
#1     4    26.7
#2     6    19.7
#3     8    15.1

Notice that you are passing all the variables without quotes and the group-column (cyl), the column which is being aggregated (mpg), the name of new column (new_mpg) are all dynamic. This would just be one use-case of it.

To learn more refer to:

  • Programming with dplyr
  • rlang 0.4.0: curly-curly
like image 78
Ronak Shah Avatar answered Nov 08 '22 19:11

Ronak Shah