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

pmap variants not handling .l properly #11

Closed
jimjam-slam opened this issue Jul 8, 2019 · 3 comments
Closed

pmap variants not handling .l properly #11

jimjam-slam opened this issue Jul 8, 2019 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@jimjam-slam
Copy link
Owner

It seems like the map variants are mishandling .l somehow. The following error suggests that purrr::pmap() is trying to iterate over .l, rather than iterate down the elements of .l in parallel:

Column newcol must be length 32 (the number of rows) or one, not 3

library(tidyverse)
library(collateral)

test <- mtcars %>% as_tibble()
test
#> # A tibble: 32 x 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ... with 22 more rows

# native pmap works fine
test %>% mutate(
  newcol = pmap(
    select(., vs, am, gear),
    function(vs, am , gear) { vs + am + gear } ))
#> # A tibble: 32 x 12
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb newcol
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4 <dbl ~
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4 <dbl ~
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1 <dbl ~
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1 <dbl ~
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2 <dbl ~
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1 <dbl ~
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4 <dbl ~
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2 <dbl ~
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2 <dbl ~
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4 <dbl ~
#> # ... with 22 more rows
test %>% mutate(
  newcol = pmap_dbl(
    select(., vs, am, gear),
    function(vs, am , gear) { vs + am + gear } ))
#> # A tibble: 32 x 12
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb newcol
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4      5
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4      5
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1      6
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1      4
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2      3
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1      4
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4      3
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2      5
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2      5
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4      5
#> # ... with 22 more rows

# collateral variants do not
test %>% mutate(
  newcol = pmap_safely(
    select(., vs, am, gear),
    function(vs, am , gear) { vs + am + gear } ))
#> Error: Column `newcol` must be length 32 (the number of rows) or one, not 3
test %>% mutate(
  newcol = pmap_quietly(
    select(., vs, am, gear),
    function(vs, am , gear) { vs + am + gear } ))
#> Error in .f(...): argument "am" is missing, with no default
test %>% mutate(
  newcol = pmap_peacefully(
    select(., vs, am, gear),
    function(vs, am , gear) { vs + am + gear } ))
#> Error: Column `newcol` must be length 32 (the number of rows) or one, not 3

Created on 2019-07-08 by the reprex package (v0.3.0)

@jimjam-slam jimjam-slam added the bug Something isn't working label Jul 8, 2019
@jimjam-slam jimjam-slam self-assigned this Jul 8, 2019
@jimjam-slam
Copy link
Owner Author

jimjam-slam commented Jul 8, 2019

More tests:

library(purrr)
library(collateral)

list1 = 1:5
list2 = c('red', 'blue', 'yellow', 'green', 'purple')
list3 = 6:10
all_lists = list(list1, list2, list3)

# classic pmap or pmap_chr works regardless of how function is specified

pmap(all_lists, function(list1, list2, list3) { paste(list1, list2, list3) })
#> [[1]]
#> [1] "1 red 6"
#> 
#> [[2]]
#> [1] "2 blue 7"
#> 
#> [[3]]
#> [1] "3 yellow 8"
#> 
#> [[4]]
#> [1] "4 green 9"
#> 
#> [[5]]
#> [1] "5 purple 10"
pmap(all_lists, ~ paste(..1, ..2, ..3))
#> [[1]]
#> [1] "1 red 6"
#> 
#> [[2]]
#> [1] "2 blue 7"
#> 
#> [[3]]
#> [1] "3 yellow 8"
#> 
#> [[4]]
#> [1] "4 green 9"
#> 
#> [[5]]
#> [1] "5 purple 10"
pmap_chr(all_lists, ~ paste(..1, ..2, ..3))
#> [1] "1 red 6"     "2 blue 7"    "3 yellow 8"  "4 green 9"   "5 purple 10"

# collateral variants do not, although they handle the errors themselves
# in this case

safe_test <- pmap_safely(all_lists, ~ paste(..1, ..2, ..3))
quiet_test <- pmap_quietly(all_lists, ~ paste(..1, ..2, ..3))
#> Error in paste(..1, ..2, ..3): the ... list contains fewer than 2 elements
peaceful_test <- pmap_peacefully(all_lists, ~ paste(..1, ..2, ..3))

safe_test
#> _ E
#> _ E
#> _ E
str(safe_test)
#> List of 3
#>  $ :List of 2
#>   ..$ result: NULL
#>   ..$ error :List of 2
#>   .. ..$ message: chr "the ... list contains fewer than 2 elements"
#>   .. ..$ call   : language paste(..1, ..2, ..3)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  $ :List of 2
#>   ..$ result: NULL
#>   ..$ error :List of 2
#>   .. ..$ message: chr "the ... list contains fewer than 2 elements"
#>   .. ..$ call   : language paste(..1, ..2, ..3)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  $ :List of 2
#>   ..$ result: NULL
#>   ..$ error :List of 2
#>   .. ..$ message: chr "the ... list contains fewer than 2 elements"
#>   .. ..$ call   : language paste(..1, ..2, ..3)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  - attr(*, "class")= chr [1:2] "safely_mapped" "list"

quiet_test
#> Error in eval(expr, envir, enclos): object 'quiet_test' not found
str(quiet_test)
#> Error in str(quiet_test): object 'quiet_test' not found

peaceful_test
#> _ O _ _ E
#> _ O _ _ E
#> _ O _ _ E
str(peaceful_test)
#> List of 3
#>  $ :List of 5
#>   ..$ result  : NULL
#>   ..$ output  : chr ""
#>   ..$ warnings: chr(0) 
#>   ..$ messages: chr(0) 
#>   ..$ error   :List of 2
#>   .. ..$ message: chr "the ... list contains fewer than 2 elements"
#>   .. ..$ call   : language paste(..1, ..2, ..3)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  $ :List of 5
#>   ..$ result  : NULL
#>   ..$ output  : chr ""
#>   ..$ warnings: chr(0) 
#>   ..$ messages: chr(0) 
#>   ..$ error   :List of 2
#>   .. ..$ message: chr "the ... list contains fewer than 2 elements"
#>   .. ..$ call   : language paste(..1, ..2, ..3)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  $ :List of 5
#>   ..$ result  : NULL
#>   ..$ output  : chr ""
#>   ..$ warnings: chr(0) 
#>   ..$ messages: chr(0) 
#>   ..$ error   :List of 2
#>   .. ..$ message: chr "the ... list contains fewer than 2 elements"
#>   .. ..$ call   : language paste(..1, ..2, ..3)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  - attr(*, "class")= chr [1:2] "peacefully_mapped" "list"

Created on 2019-07-08 by the reprex package (v0.3.0)

@jimjam-slam
Copy link
Owner Author

Think I've caught it: the pmap_*() functions were hooked up to map_wrapper(), rather than pmap_wrapper(). Implementing fix now.

@jimjam-slam
Copy link
Owner Author

Fixed in dev 🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant