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

automatically name unnamed pluckers in hoist #837

Closed
mgirlich opened this issue Dec 13, 2019 · 6 comments
Closed

automatically name unnamed pluckers in hoist #837

mgirlich opened this issue Dec 13, 2019 · 6 comments
Labels
feature a feature request or enhancement rectangling 🗄️ converting deeply nested lists into tidy data frames
Milestone

Comments

@mgirlich
Copy link
Contributor

mgirlich commented Dec 13, 2019

Often one wants to name the columns produced by hoist() the same as the element in the list. So to make hoist() less verbose and more comfortable for interactive use I propose automatically naming the column as follows for unnamed arguments:

  • if the last part of the pluck specification is a character use that
  • if the last part of the pluck specification is a number paste the number to the last character part of the spec, e.g. list("film", 1) would produce the column name film_1.
@mgirlich mgirlich changed the title automatically name any unnamed pluckers automatically name unnamed pluckers in hoist Dec 13, 2019
@hadley
Copy link
Member

hadley commented Apr 1, 2020

This is an interesting idea. What would you do if the pluck spec is a number? Or the number comes first?

@hadley hadley added feature a feature request or enhancement rectangling 🗄️ converting deeply nested lists into tidy data frames labels Apr 1, 2020
@mgirlich
Copy link
Contributor Author

mgirlich commented Apr 2, 2020

maybe there should be a short and a long name option, e.g. something like

pluck spec short name long name
list("people", "name") name people_name
list("people", "name", 1) name_1 people_name_1
list(1) ..1 ..1
list(1, 2) ..1..2 ..1..2
list(1, "people", "name") ..1_name ..1_people_name

as the numbers in a spec will probably be small one might also use words for the, i.e. list(1, "name") gives first_name

@hadley
Copy link
Member

hadley commented Apr 2, 2020

That's starting to feel a bit too magical too me — I think there'd have to be one principle applied consistently.

@mgirlich
Copy link
Contributor Author

mgirlich commented Apr 2, 2020

Looking at it again I agree that it looks quite magical.

One easy principle would be to simply use paste(pluck_spec, collapse = "_") for the long name version. Unfortunately, this generates non-syntactic names for leading numbers. But this could be resolved by adding a names_repair argument, so the user can choose how to handle this.

For short names one would use basically the same strategy but keep all trailing numbers, i.e.
list("people", "name", 1, 2) would give name_1_2.

Do you like that better?

@jennybc
Copy link
Member

jennybc commented Apr 3, 2020

The case that seems like a no-brainer and a great place to start is when you pluck a string. That covers so much of the pain IMO. This is the case where I most feel like hoist() should be able to figure this out.

That being said, I feel like we discussed this once in slack @hadley, and you objected? There is some potential for muddying the waters re: our quoting conventions.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 3.6.2

df <- tibble(
  character = c("Toothless", "Dory"),
  metadata = list(
    list(
      species = "dragon",
      color = "black",
      films = c(
        "How to Train Your Dragon",
        "How to Train Your Dragon 2",
        "How to Train Your Dragon: The Hidden World"
      )
    ),
    list(
      species = "blue tang",
      color = "blue",
      films = c("Finding Nemo", "Finding Dory")
    )
  )
)

df %>% 
  hoist(metadata, species = "species", color = "color")
#> # A tibble: 2 x 4
#>   character species   color metadata        
#>   <chr>     <chr>     <chr> <list>          
#> 1 Toothless dragon    black <named list [1]>
#> 2 Dory      blue tang blue  <named list [1]>

Created on 2020-04-03 by the reprex package (v0.3.0.9001)

@hadley hadley added this to the v1.1.0 milestone Apr 21, 2020
@hadley
Copy link
Member

hadley commented Apr 21, 2020

Ok, let's start with just naming single string pluckers.

@hadley hadley closed this as completed in 4ebdd1f Apr 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement rectangling 🗄️ converting deeply nested lists into tidy data frames
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants