Skip to content

pivot_wider names_repair custom function does not replace names #1107

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

Closed
bwbioinfo opened this issue Mar 11, 2021 · 2 comments · Fixed by #1236
Closed

pivot_wider names_repair custom function does not replace names #1107

bwbioinfo opened this issue Mar 11, 2021 · 2 comments · Fixed by #1236
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@bwbioinfo
Copy link

When pivoting a column that would result in invalid names, a custom function can be provided to correct them, both the unnest and pivot_wider function direct to the vctrs::vec_as_names function. The unnest function yields the expected output, while the pivot_wider function yields the original colnames, with duplicates.


pivot_wider with custom function does not apply function results.

In the three blocks below, the colname "test" is duplicated, it is corrected in the first two (vctrs::vec_as_names, unnest) but not in the third (pivot_wider) to "test_attr".

library(magrittr)
library(tidyverse)

vctrs::vec_as_names(
    c("test","test2", "test"),
    repair = 
        function(x) {
            print(duplicated(x))
            x[duplicated(x)]
            ifelse(
                duplicated(x),
                paste0(x, "_attr"),
                x
            )
        }
)
#> [1] FALSE FALSE  TRUE
#> [1] "test"      "test2"     "test_attr"

tribble(
    ~test, ~data,
    "a", tibble(test = 1),
    "b", tibble(test1 =  2)
) %>%
    unnest(
        data,
        names_repair = 
            function(x) {
                names = ifelse(
                    duplicated(x),
                    paste0(x, "_attr"),
                    x
                )
                
                print(names)
                
                return(names)
            }
        )
#> [1] "test"      "test_attr" "test1"
#> # A tibble: 2 x 3
#>   test  test_attr test1
#>   <chr>     <dbl> <dbl>
#> 1 a             1    NA
#> 2 b            NA     2

tribble(
    ~test, ~names, ~values,
    "a", "test", 1,
    "b", "test2", 2
    ) %>% 
    pivot_wider(
        names_from = names,
        values_from = values,
        names_repair = 
            function(x) {
                names = ifelse(
                    duplicated(x),
                    paste0(x, "_attr"),
                    x
                )
                
                print(names)
                
                return(names)
            }
        )
#> [1] "test"      "test_attr" "test2"
#> # A tibble: 2 x 3
#>   test  test  test2
#>   <chr> <chr> <dbl>
#> 1 a     a        NA
#> 2 b     b         2

R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252   
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tidyr_1.1.3    stringr_1.4.0  purrr_0.3.4    dplyr_1.0.5   
[5] tibble_3.1.0   readr_1.4.0    magrittr_2.0.1
@hadley
Copy link
Member

hadley commented Aug 23, 2021

Somewhat more minimal reprex:

library(tidyr)

df <- tribble(
    ~test, ~names, ~values,
    "a", "test", 1,
    "b", "test2", 2
)

df %>% pivot_wider(names_from = names, values_from = values, names_repair = ~ make.unique(.x))
#> # A tibble: 2 × 3
#>   test  test  test2
#>   <chr> <chr> <dbl>
#> 1 a     a        NA
#> 2 b     b         2

Created on 2021-08-23 by the reprex package (v2.0.0)

@hadley hadley added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" labels Aug 23, 2021
@DavisVaughan
Copy link
Member

Seems to have to do with:

tidyr/R/pivot-wide.R

Lines 282 to 288 in 0e57934

out <- wrap_error_names(vec_cbind(as_tibble(rows), !!!value_out, .name_repair = names_repair))
# recreate desired column order
# https://github.com/r-lib/vctrs/issues/227
if (all(spec$.name %in% names(out))) {
out <- out[c(names(rows), spec$.name)]
}

where the re-order uses names before name repair was applied.

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 pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants