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

Hoist removes duplicated names from list - even if they are not targeted at all #1259

Closed
LukasWallrich opened this issue Dec 10, 2021 · 1 comment · Fixed by #1262
Closed

Comments

@LukasWallrich
Copy link

LukasWallrich commented Dec 10, 2021

I am aware that hoist does not like duplicated names in lists. However, I now came across a behaviour (2 below) that led to a lot of debugging effort and should - in my view - either be changed or at least trigger a warning.

  1. Requesting a duplicated name

This extracts "name" and silently deletes "idea" - I didn't expect that, but it is maybe the expected behaviour. However, I would suggest to at least document this explicitly.

tibble(x = list(list("my" = "name", "my" = "idea", "your" = "victory"))) %>% hoist(x, "my")

2) Requesting another name with hoist - the real issue

This extracts "victory" and silently drops "idea" - in my understanding, all list elements but ["your"] should be left alone ... anything else should at least trigger a warning?

tibble(x = list(list("my" = "name", "my" = "idea", "your" = "victory"))) %>% hoist(x, "your")
@DavisVaughan
Copy link
Member

#1262 fixes the two issues here. It is still not completely perfect, but I think this is a big improvement for a small amount of work, and fixing the last issue shown below is much harder

library(tidyr)

df <- tibble(x = list(list("my" = "name", "my" = "idea", "your" = "victory")))

# First issue, fixed
hoist(df, x, "my")
#> # A tibble: 1 × 2
#>   my    x               
#>   <chr> <list>          
#> 1 name  <named list [2]>
hoist(df, x, "my")$x
#> [[1]]
#> [[1]]$my
#> [1] "idea"
#> 
#> [[1]]$your
#> [1] "victory"

# Second issue, fixed
hoist(df, x, "your")
#> # A tibble: 1 × 2
#>   your    x               
#>   <chr>   <list>          
#> 1 victory <named list [2]>
hoist(df, x, "your")$x
#> [[1]]
#> [[1]]$my
#> [1] "name"
#> 
#> [[1]]$my
#> [1] "idea"

# This doesn't work right, but is hard to fix
hoist(df, x, my1 = "my", my2 = "my")
#> # A tibble: 1 × 3
#>   my1   my2   x               
#>   <chr> <chr> <list>          
#> 1 name  name  <named list [1]>
hoist(df, x, my1 = "my", my2 = "my")$x
#> [[1]]
#> [[1]]$your
#> [1] "victory"

# Do this instead for now if you know you need both
hoist(hoist(df, x, my1 = "my"), x, my2 = "my")
#> # A tibble: 1 × 3
#>   my1   my2   x               
#>   <chr> <chr> <list>          
#> 1 name  idea  <named list [1]>

Created on 2021-12-13 by the reprex package (v2.0.1)

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

Successfully merging a pull request may close this issue.

2 participants