-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
maps and pmaps function in the new version of purrr not works when ... in the function #367
Comments
I have a probably related issue: the function library("purrr")
#> Warning: package 'purrr' was built under R version 3.4.1
# R version 3.4.0 (2017-04-21)
# purrr_0.2.3
safe_sum_explicit_param <- function(numbers) {
possibly(
~ sum(numbers),
otherwise = -Inf
)
}
safe_sum_explicit_param(c(a = 2, b = 3))()
#> [1] 5
safe_sum_explicit_param("not a vector of numbers, error is expected")()
#> [1] -Inf
safe_sum_ellipsis_param <- function(...) {
possibly(
~ sum(...),
otherwise = -Inf
)
}
safe_sum_ellipsis_param(a = 2, b = 3)()
#> [1] 0
# 0 means empty param list passed to ...
safe_sum_good <- function(...) {
params <- list(...)
possibly(
~ sum(unlist(params)),
otherwise = -Inf
)
}
safe_sum_good(a = 2, b = 3)()
#> [1] 5
safe_sum_good(a = "not a vector of numbers, error is expected")()
#> [1] -Inf I would expect passing |
I experimented with library("purrr")
#> Warning: package 'purrr' was built under R version 3.4.1
# R version 3.4.0 (2017-04-21)
# purrr_0.2.3
sum_with_default_formula_notation <- function(...) {
map(1:3, ~ sum(.x, ...))
}
sum_with_default_formula_notation(a = 1000)
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 4
#>
#> [[3]]
#> [1] 6
sum_with_default_anonymous_function <- function(...) {
map(1:3, function(default) {sum(default, ...)})
}
sum_with_default_anonymous_function(a = 1000)
#> [[1]]
#> [1] 1001
#>
#> [[2]]
#> [1] 1002
#>
#> [[3]]
#> [1] 1003
sum_with_default_workaround <- function(...) {
params <- list(...)
map(1:3, ~ sum(.x, unlist(params)))
}
sum_with_default_workaround(a = 1000)
#> [[1]]
#> [1] 1001
#>
#> [[2]]
#> [1] 1002
#>
#> [[3]]
#> [1] 1003 |
Yes, if you want to use |
list %>% pmaps(fun_(x=.,y=3,...))
as above, when there is "..." in the fun_ ,the second parameter y will not be passed correctly, while the old version works well.
The text was updated successfully, but these errors were encountered: