One of my R scripts has a line of code in the following syntax (simplified into a reprex) which I discovered from a Stack Overflow thread.
library(tidyverse, warn.conflicts = FALSE)
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>%
{ map2(.$x, .$y, sum) }
#> [[1]]
#> [1] 5
#>
#> [[2]]
#> [1] 7
#>
#> [[3]]
#> [1] 3
#>
#> [[4]]
#> [1] 7
#>
#> [[5]]
#> [1] 9
#>
#> [[6]]
#> [1] 5
#>
#> [[7]]
#> [1] 13
#>
#> [[8]]
#> [1] 15
#>
#> [[9]]
#> [1] 11
Created on 2020-04-06 by the reprex package (v0.3.0)
I'm generating all possible combinations of two input variables and passing them as arguments to a custom function. This gives me the desired result but I don't really understand what the curly braces do and how this differs from "normal" ways of writing piped code in the tidyverse. Can someone explain?
And is this really an elegant approach? Can this be re-written in a better way?