You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.2df<- 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)
Activity
[-]automatically name any unnamed pluckers[/-][+]automatically name unnamed pluckers in hoist[/+]hadley commentedon Apr 1, 2020
This is an interesting idea. What would you do if the pluck spec is a number? Or the number comes first?
mgirlich commentedon Apr 2, 2020
maybe there should be a short and a long name option, e.g. something like
list("people", "name")list("people", "name", 1)list(1)list(1, 2)list(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")givesfirst_namehadley commentedon 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 commentedon 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 anames_repairargument, 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 givename_1_2.Do you like that better?
jennybc commentedon 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.
Created on 2020-04-03 by the reprex package (v0.3.0.9001)
hadley commentedon Apr 21, 2020
Ok, let's start with just naming single string pluckers.
Automatically name single-string pluckers in hoist()