mgirlich / tibblify

Rectangle Nested Lists
https://mgirlich.github.io/tibblify/
GNU General Public License v3.0
67 stars 2 forks source link

Support recursive fields #155

Closed mgirlich closed 2 years ago

mgirlich commented 2 years ago

How could the spec look like for recursive fields, e.g.

x <- list(
  list(
    id = 1,
    name = "a",
    children = list(
      list(id = 11, name = "b", children = NULL),
      list(
        id = 12,
        name = "c",
        children = list(
          list(id = 121, name = "d", children = NULL),
          list(id = 122, name = "e", children = NULL)
        )
      )
    )
  )
)

Output formats:

spec idea:

tib_df(
  tib_int("id"),
  tib_chr("name"),
  recursive = tibblify_recursive(
    output = c("flat", "nested"),
    child = "children", # recursive column
    id = "id", # Required for flat format; id column
    parent = "parent", # Optional: name of column with reference to parent id
    ancestor = "path" # Optional: name of column with ancestor ids
  )
)

Output

tibble::tribble(
  ~ id, ~ name, ~ parent,      ~ path,
     1,    "a",       NA,        NULL,
    11,    "b",        1,     list(1),
    12,    "c",        1,     list(1),
   121,    "d",       12, list(1, 12),
   122,    "e",       12, list(1, 12),
)
mgirlich commented 2 years ago

@krlmlr I think you also mentioned recursive fields at some point. Do you see other requirements?

mgirlich commented 2 years ago

Recursive fields should - at least for now - not use list_of due to a) huge performance issues with list_of b) different depths