r4fun / hierplane

🌳 Hierplane for R
https://r4fun.github.io/hierplane/
Other
9 stars 0 forks source link

Add sample datasets #35

Closed mathidachuk closed 4 years ago

mathidachuk commented 4 years ago

Addresses #11

Please add org chart dataset!

tylerlittlefield commented 4 years ago

I swear I am trying! Unnesting json is hard 😢

library(jsonlite)
library(tibble)
library(tidyr)
library(dplyr)

df <- "/Users/tylerlittlefield/Desktop/organization.json" %>% 
  read_json()

df$root %>% 
  as_tibble() %>% 
  unnest_longer(attributes) %>% 
  unnest_longer(children)
#> # A tibble: 24 x 5
#>    word          attributes nodeType children     children_id
#>    <chr>         <chr>      <chr>    <named list> <chr>      
#>  1 Esther Walker CEO        root     <chr [1]>    word       
#>  2 Esther Walker CEO        root     <chr [1]>    link       
#>  3 Esther Walker CEO        root     <list [1]>   attributes 
#>  4 Esther Walker CEO        root     <chr [1]>    nodeType   
#>  5 Esther Walker CEO        root     <chr [1]>    word       
#>  6 Esther Walker CEO        root     <chr [1]>    link       
#>  7 Esther Walker CEO        root     <chr [1]>    nodeType   
#>  8 Esther Walker CEO        root     <list [4]>   children   
#>  9 Esther Walker CEO        root     <chr [1]>    word       
#> 10 Esther Walker CEO        root     <chr [1]>    link       
#> # … with 14 more rows

Created on 2020-07-13 by the reprex package (v0.3.0)

Gettings closer? Maybe??????

library(hierplane)
library(jsonlite)
library(tibble)
library(tidyr)
library(dplyr)
library(purrr)

df <- "/Users/tylerlittlefield/Desktop/organization.json" %>% 
  read_json()

level1 <- df$root %>% 
  as_tibble() %>% 
  unnest_longer(attributes) %>% 
  select(children) %>% 
  unnest_wider(children) %>% 
  unnest_longer(attributes) %>% 
  unnest_longer(children)

level2 <- level1 %>% 
  select(children) %>% 
  unnest_wider(children)

level3 <- level2 %>% 
  select(children) %>% 
  unnest_auto(children) %>% 
  unnest_wider(children) %>% 
  unnest_longer(attributes)

level4 <- level3 %>% 
  select(children) %>% 
  unnest_auto(children) %>% 
  unnest_wider(children) %>% 
  unnest_auto(attributes) %>% 
  drop_na()

level1[-5] %>% 
  bind_rows(level2[-4]) %>% 
  bind_rows(level3[-5]) %>% 
  bind_rows(level4) %>% 
  bind_rows(
    tibble(
      word = "Esther Walker",
      attributes = "CEO",
      nodeType = "root"
    )
  ) %>% 
  distinct()
tylerlittlefield commented 4 years ago

Merging, we can continue to submit PR's for additional datasets when we have them available.