I thought I had understood how to use the additional arguments argument (...) of purrr::map. Here is some code that hopefully illustrates the (to me) unexpected behaviour of purrr::map:
It seems that passing argument a as additional argument in purrr::map is not working:
library(purrr)
f <- function(a, b) {
a + b
}
g <- function(a = 0, b) {
a + b
}
map(1:3, .f = ~ f(b = .x, a = 1))
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 4
map(1:3, .f = ~ f(b = .x), a = 1)
#> Error in f(b = .x): argument "a" is missing, with no default
map(1:3, .f = ~ g(b = .x, a = 1))
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 4
map(1:3, .f = ~ g(b = .x), a = 1)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
lapply(1:3, function(b, a = 1) f(a, b))
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 4
lapply(1:3, function(b, a) f(a, b), a = 1)
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 4
My question is why does the code:
map(1:3, .f = ~ f(b = .x), a = 1)
throw an error?
lappymap(1:3, .f = function(b, a = 1) f(a, b))a=1outside the lambda definition is throwing an error. The question you linked is focused on how to forward the argument, but doesn't really explain the "why".