Skip to content
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

Closed
earthcli opened this issue Aug 13, 2017 · 3 comments

Comments

@earthcli
Copy link

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.

@czeildi
Copy link
Contributor

czeildi commented Aug 16, 2017

I have a probably related issue: the function possibly does not work with the ellipsis and this behavior changed since the previous version (from 0.2.2 to 0.2.3) Here is a reprex containing a work-around as well as the issue:

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 ... to work without any problem as it is often useful when creating general functions using purrr.

@czeildi
Copy link
Contributor

czeildi commented Aug 16, 2017

I experimented with map and ... and found examples which show that the issue is probably related to the formula notation:

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

@hadley
Copy link
Member

hadley commented Feb 5, 2018

Yes, if you want to use ..., you'll need to use an anonymous function. ~ is only suitable for use for simple functions.

@hadley hadley closed this as completed Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants