r4fun / hierplane

šŸŒ³ Hierplane for R
https://r4fun.github.io/hierplane/
Other
9 stars 0 forks source link

Add hp_ functions and reduce hierplane to a single function #24

Closed tylerlittlefield closed 4 years ago

tylerlittlefield commented 4 years ago

Examples

Hierplane objects print differently now, they show that they are a hierplane object and from what function they were created:

# devtools::build(".") # do this first
library(hierplane)

# data
df <- structure(
  list(
    parent_id = c("Bob", "Bob", "Bob", "Bob", "Angelica", "Bob"),
    child_id = c("Bob", "Angelica", "Eliza", "Peggy", "John", "Jess"),
    child = c("Bob", "Angelica", "Eliza", "Peggy", "John", "Jess"),
    node_type = c("gen1", "gen2", "gen2", "gen2", "gen3", "ext"),
    link = c("ROOT", "daughter", "daughter", "daughter", "son", "cousin"),
    height = c("100 cm", "100 cm", "90 cm", "50 cm", "10 cm", "70 cm"),
    age = c("60 yo", "30 yo", "25 yo", "10 yo", "0.5 yo", "150 yo")
  ),
  row.names = c(NA, -6L),
  class = c("data.frame")
)

# create a hierplane object from dataframe input
df_dataframe <- hp_dataframe(
  .data = df,
  title = "Family Tree",
  settings = hierplane_settings(
    attributes = c("height", "age")
  )
)

df_dataframe
#> <hierplane_object: from hp_dataframe>

Then to render we just call hierplane():

# render
hierplane(df_dataframe)

If you want to print the json or the list, just use jsonlite:

# print the json
jsonlite::prettify(df_dataframe)

# convert to list
jsonlite::fromJSON(df_dataframe)

With spacyr we do this:

# create a hierplane object from character input using spacyr
df_spacyr <- hp_spacyr(.data = "Sam likes boats")
df_spacyr
#> <hierplane_object: from hp_spacyr>

hierplane(df_spacyr)

With styles:

# using styles
node_type_to_style <- list(
  "gen1" = c("color1", "strong"),
  "gen2" = "color6",
  "gen3" = "color4",
  "ext" = "placeholder"
)

link_name_to_label <- list(
  daughter = "dtr", 
  son = "son", 
  cousin = "cuz"
)

link_to_positions <- list(cousin = "right")

settings_style <- add_styles(
  settings = hierplane_settings(
    attributes = c("height", "age"),
  ),
  node_type_to_style = node_type_to_style,
  link_to_positions = link_to_positions,
  link_name_to_label = link_name_to_label
)

# create a hierplane object from dataframe input
df_dataframe <- hp_dataframe(
  .data = df,
  title = "Family Tree",
  settings = settings_style
)

# render
hierplane(df_dataframe, theme = "dark") # need to fix theme light

This closes #21

tylerlittlefield commented 4 years ago

I made some changes, hp_dataframe has title set to "Hierplane" by default (like before). Also, hp_spacyr has settings/... params added:

library(hierplane)

df_dataframe <- hp_dataframe(
  .data = df, 
  settings = hierplane_settings(attributes = c("height", "age"))
)

hierplane(df_dataframe, theme = "dark")

df <- structure(
  list(
    parent_id = c("Bob", "Bob", "Bob", "Bob", "Angelica", "Bob"),
    child_id = c("Bob", "Angelica", "Eliza", "Peggy", "John", "Jess"),
    child = c("Bob", "Angelica", "Eliza", "Peggy", "John", "Jess"),
    node_type = c("gen1", "gen2", "gen2", "gen2", "gen3", "ext"),
    link = c("ROOT", "daughter", "daughter", "daughter", "son", "cousin"),
    height = c("100 cm", "100 cm", "90 cm", "50 cm", "10 cm", "70 cm"),
    age = c("60 yo", "30 yo", "25 yo", "10 yo", "0.5 yo", "150 yo")
  ),
  row.names = c(NA, -6L),
  class = c("data.frame")
)
tylerlittlefield commented 4 years ago

One more change to class name:

hierplane::hp_spacyr("sam likes boats")
#> <hierplane_tree object: from hp_spacyr>

Please let me know if there is anything else I need to address šŸ™

mathidachuk commented 4 years ago

Works perfectly now! The only thing is that we need to export spacyr_default() (also may need to rename to something more indicative that this defines the hierplane settins for spacyr hps). We can address this in a different pr. I'll open a ticket for documentation.

Also need to export %>% :D

Approving and merging!

# create a hierplane object from dataframe input
df_dataframe <- hp_dataframe(
  .data = df,
  title = "Family",
  settings = hierplane_settings(
    attributes = c("height", "age")
  ) %>% add_styles(link_to_positions = list(cousin = "right"))
)

hierplane(df_dataframe)

df_spacyr <- hp_spacyr("Bob says hi", 
                       settings = 
                         hierplane:::spacyr_default() %>% 
                           add_styles(
                             link_to_positions = 
                               list(
                                 nsubj = "left",
                                 intj = "right"
                               )
                           )
                       )

hierplane(df_spacyr, theme = "dark")