purrr
purrr copied to clipboard
`map()` show different results with `.f = ~ foo(.x)` and `.f = ~ foo(.)`
library(magrittr)
set.seed(42)
df1 <- data.frame(a = rnorm(1), b = rnorm(1))
df2 <- data.frame(a = runif(2), b = runif(2))
(df_list <- list(df1, df2))
# doesn't work
df_list %>% purrr::map(~ . %>% dplyr::select("a"))
df_list %>% purrr::map(\(.) . %>% dplyr::select("a"))
df_list %>% purrr::map(function(.) . %>% dplyr::select("a"))
# no pipe
df_list %>% purrr::map(~ dplyr::select(., "a"))
df_list %>% purrr::map(\(.) dplyr::select(., "a"))
# works, what's going here?
df_list %>% purrr::map(~ .x %>% dplyr::select("a"))
df_list %>% purrr::map(\(x) x %>% dplyr::select("a"))
df_list %>% purrr::map(function(x) x %>% dplyr::select("a"))
My guess is the .
usage interferes with {magrittr} pipe %>%
.
?purrr::map()
If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:
- For a single argument function, use .
- For a two argument function, use .x and .y
- For more arguments, use ..1, ..2, ..3 etc
This syntax allows you to create very compact anonymous functions.
What's the use case of the 2nd and 3rd bullet points?
reprex results
library(magrittr)
set.seed(42)
df1 <- data.frame(a = rnorm(1), b = rnorm(1))
df2 <- data.frame(a = runif(2), b = runif(2))
(df_list <- list(df1, df2))
#> [[1]]
#> a b
#> 1 1.370958 -0.5646982
#>
#> [[2]]
#> a b
#> 1 0.6417455 0.7365883
#> 2 0.5190959 0.1346666
# doesn't work
df_list %>% purrr::map(~ . %>% dplyr::select("a"))
#> [[1]]
#> Functional sequence with the following components:
#>
#> 1. dplyr::select(., "a")
#>
#> Use 'functions' to extract the individual functions.
#>
#> [[2]]
#> Functional sequence with the following components:
#>
#> 1. dplyr::select(., "a")
#>
#> Use 'functions' to extract the individual functions.
df_list %>% purrr::map(\(.) . %>% dplyr::select("a"))
#> [[1]]
#> Functional sequence with the following components:
#>
#> 1. dplyr::select(., "a")
#>
#> Use 'functions' to extract the individual functions.
#>
#> [[2]]
#> Functional sequence with the following components:
#>
#> 1. dplyr::select(., "a")
#>
#> Use 'functions' to extract the individual functions.
df_list %>% purrr::map(function(.) . %>% dplyr::select("a"))
#> [[1]]
#> Functional sequence with the following components:
#>
#> 1. dplyr::select(., "a")
#>
#> Use 'functions' to extract the individual functions.
#>
#> [[2]]
#> Functional sequence with the following components:
#>
#> 1. dplyr::select(., "a")
#>
#> Use 'functions' to extract the individual functions.
# no pipe
df_list %>% purrr::map(~ dplyr::select(., "a"))
#> [[1]]
#> a
#> 1 1.370958
#>
#> [[2]]
#> a
#> 1 0.6417455
#> 2 0.5190959
df_list %>% purrr::map(\(.) dplyr::select(., "a"))
#> [[1]]
#> a
#> 1 1.370958
#>
#> [[2]]
#> a
#> 1 0.6417455
#> 2 0.5190959
# what's going here?
df_list %>% purrr::map(~ .x %>% dplyr::select("a"))
#> [[1]]
#> a
#> 1 1.370958
#>
#> [[2]]
#> a
#> 1 0.6417455
#> 2 0.5190959
df_list %>% purrr::map(\(x) x %>% dplyr::select("a"))
#> [[1]]
#> a
#> 1 1.370958
#>
#> [[2]]
#> a
#> 1 0.6417455
#> 2 0.5190959
df_list %>% purrr::map(function(x) x %>% dplyr::select("a"))
#> [[1]]
#> a
#> 1 1.370958
#>
#> [[2]]
#> a
#> 1 0.6417455
#> 2 0.5190959
Created on 2022-06-10 by the reprex package (v2.0.1)