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

.id argument ignored in imap_dfr()/imap_dfc()? #429

Closed
eaurele opened this issue Dec 18, 2017 · 4 comments · Fixed by #454
Closed

.id argument ignored in imap_dfr()/imap_dfc()? #429

eaurele opened this issue Dec 18, 2017 · 4 comments · Fixed by #454
Labels
bug an unexpected problem or unintended behavior

Comments

@eaurele
Copy link

eaurele commented Dec 18, 2017

purrr/R/imap.R

Line 50 in 62b135a

#' @rdname imap

imap_dfr <- function(.x, .f, ..., .id = NULL) {
  .f <- as_mapper(.f, ...)
  map2_dfr(.x, vec_index(.x), .f, ...)
}

I don't seem to be able to make use of .id in imap_dfr(). Looking at the source code, the .id arg seems not to be handled at all. Is it the case or am I missing something?

@cderv
Copy link
Contributor

cderv commented Dec 18, 2017

It seems you are right. To illustrate, here is a dummy reprex with a custom to argument fonction to use with imap and map2

library(purrr)
library(tibble)

as_tibble_custom <- function(value, name = NULL) {
    tab <- as_tibble(value)
    if (! is.null(name)) {
        tab$col_id = paste0("col_", name)
    }
    tab
}
# same as imap and it works
list(a = 1, b = 2, c = 3) %>%
    map2_dfr(names(.), as_tibble_custom)
#> # A tibble: 3 x 2
#>   value col_id
#>   <dbl>  <chr>
#> 1     1  col_a
#> 2     2  col_b
#> 3     3  col_c
list(a = 1, b = 2, c = 3) %>%
    map2_dfr(names(.), as_tibble_custom, .id = "name")
#> # A tibble: 3 x 3
#>    name value col_id
#>   <chr> <dbl>  <chr>
#> 1     a     1  col_a
#> 2     b     2  col_b
#> 3     c     3  col_c
# with imap it does not work
list(a = 1, b = 2, c = 3) %>%
    imap_dfr(as_tibble_custom)
#> # A tibble: 3 x 2
#>   value col_id
#>   <dbl>  <chr>
#> 1     1  col_a
#> 2     2  col_b
#> 3     3  col_c
list(a = 1, b = 2, c = 3) %>%
    imap_dfr(as_tibble_custom, .id = "name")
#> # A tibble: 3 x 2
#>   value col_id
#>   <dbl>  <chr>
#> 1     1  col_a
#> 2     2  col_b
#> 3     3  col_c

In the source code, .id is not pass to map2_dfr as you pointed out.

Moreover, imap_dfc has also a .id argument, but it should not as map2_dfc does not have one.

@hadley
Copy link
Member

hadley commented Feb 4, 2018

Would either of you like to do a PR? This would be a great first PR if you haven't done one before.

@hadley hadley added the bug an unexpected problem or unintended behavior label Feb 4, 2018
@maxheld83
Copy link

is it possible that this hasn't been pushed to CRAN, or that there has been a reversion?
I would think that with a merge on Feb 5, that this fix should be part of the current 2.5.x release.

As expected, the above reprex from @cderv currently works off of github (the last) call is the interesting one):

devtools::install_github(repo = "tidyverse/purrr", force = TRUE)
library(purrr)
library(tibble)

as_tibble_custom <- function(value, name = NULL) {
  tab <- as_tibble(value)
  if (! is.null(name)) {
    tab$col_id = paste0("col_", name)
  }
  tab
}
# same as imap and it works
list(a = 1, b = 2, c = 3) %>%
  map2_dfr(names(.), as_tibble_custom)

list(a = 1, b = 2, c = 3) %>%
  map2_dfr(names(.), as_tibble_custom, .id = "name")

list(a = 1, b = 2, c = 3) %>%
  imap_dfr(as_tibble_custom)

list(a = 1, b = 2, c = 3) %>%
  imap_dfr(as_tibble_custom, .id = "name")

but, apparently, not off of CRAN:

install.packages("purrr")
library(purrr)
library(tibble)

as_tibble_custom <- function(value, name = NULL) {
  tab <- as_tibble(value)
  if (! is.null(name)) {
    tab$col_id = paste0("col_", name)
  }
  tab
}
# same as imap and it works
list(a = 1, b = 2, c = 3) %>%
  map2_dfr(names(.), as_tibble_custom)

list(a = 1, b = 2, c = 3) %>%
  map2_dfr(names(.), as_tibble_custom, .id = "name")

list(a = 1, b = 2, c = 3) %>%
  imap_dfr(as_tibble_custom)

# this now does not include a name col
list(a = 1, b = 2, c = 3) %>%
  imap_dfr(as_tibble_custom, .id = "name")

@cderv
Copy link
Contributor

cderv commented Jul 23, 2018

Looking at commits from tag v0.2.5 it seems CRAN version does not include any of the fixes since 0.2.4. It makes sense since it is indicated in the release pane:

This is a maintenance release following the release of dplyr 0.7.5.

However, version number in DESCRIPTION and in NEWS are not correct. Development version is less than current CRAN version. Not so clear.

I am not aware of the release process so I let the purrr dev team say if there is something off or not.

Also, I think it is a good practice to open a new issue rather than discussing in closed one. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants