r4fun / hierplane

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

Style map #22

Closed mathidachuk closed 4 years ago

mathidachuk commented 4 years ago

Add interface for passing styling options to hierplane function. Add styling options list as guidance for user.

see wip.r for list of usage examples

TODO: test + documentation.

Wanted to get your feedback on implementation before adding tests and documentation.

Closes #12

tylerlittlefield commented 4 years ago

I think this looks pretty good, if you take a look at the checks, we need to address a couple things before merging:

── R CMD check results ─────────────────────────────── hierplane 0.0.0.9000 ────
Duration: 19.5s

❯ checking for missing documentation entries ... WARNING
  Undocumented code objects:
    ‘style_options’
##[error]Error: R CMD check found WARNINGs
  Undocumented data sets:
Execution halted
    ‘style_options’
  All user-level objects in a package should have documentation entries.
  See chapter ‘Writing R documentation files’ in the ‘Writing R
  Extensions’ manual.

❯ checking data for ASCII and uncompressed saves ... WARNING
    Warning: package needs dependence on R (>= 2.10)

❯ checking top-level files ... NOTE
  Non-standard files/directories found at top level:
    ‘data-raw’ ‘wip.R’

0 errors ✔ | 2 warnings ✖ | 1 note ✖
##[error]Process completed with exit code 1.
mathidachuk commented 4 years ago

saving for laterrrr

invisible(lapply(list.files("R", full.names = T), source))

a <- as.data.frame(dplyr::tribble(
  ~parent_id,  ~child_id,    ~child,      ~node_type,     ~link,      ~height,  ~age,
  "Bob",       "Bob",        "Bob",       "gen1",         "ROOT",     "100 cm", "60 yo",
  "Bob",       "Angelica",   "Angelica",  "gen2",         "daughter", "100 cm", "30 yo",
  "Bob",       "Eliza",      "Eliza",     "gen2",         "daughter", "90 cm",  "25 yo",
  "Bob",       "Peggy",      "Peggy",     "gen2",         "daughter", "50 cm",  "10 yo",
  "Angelica",  "John",       "John",      "gen3",         "son",      "10 cm",  "0.5 yo",
  "Bob",       "Jess",       "Jess",      "ext",          "cousin",   "70 cm",  "150 yo"
))

hierplane(a, title = "Family Tree",
          settings = hierplane_settings(attributes = c("height", "age")))
hierplane_spacy("Bob has three daughters and a grandson.")

# ----
# styles from list
library(magrittr) # for pipe
node_type_to_style <- list("gen1" = c("color1", "strong"),
                           "gen2" = list("color6"),
                           "gen3" = list("color4"),
                           "ext" = list("placeholder"))
link_to_positions <- list(cousin = "right")
link_name_to_label <- list(daughter = "dtr", son = "son", cousin = "cuz")
settings <- hierplane_settings(attributes = c("height", "age")) %>%
  add_styles(
    node_type_to_style = node_type_to_style,
    link_to_positions = link_to_positions,
    link_name_to_label = link_name_to_label
  )

hierplane(a, title = "Family Tree", settings = settings, theme = "dark")

# check styling input and show warnings for unmatched values
# hierplane will still plot, but unmatched are ignored hence the warnings
# TLDR bad LHS assignments throw warnings
node_type_to_style <- list("gen1" = c("color1", "strong", "color2"),
                           "gen2" = list("color6"),
                           "gen2" = list("color4"), # repeat label
                           "ext" = list("placeholder"))
link_to_positions <- list(cousssssin = "right") # bad name
link_name_to_label <- list(daughter = "D", son = "S", son = "SS") # repeat label
settings <- hierplane_settings(attributes = c("height", "age")) %>%
  add_styles(
    node_type_to_style = node_type_to_style,
    link_to_positions = link_to_positions,
    link_name_to_label = link_name_to_label
  )

hierplane(a, title = "Family Tree", settings = settings, theme = "dark")

# check styling input and throw error mismatched styling options
# hierplane will fail to plot hence the errors
# TLDR bad RHS assignments throw errors

## example 1
node_type_to_style <- list("gen1" = c("color1", "strong", "color2"),
                           "gen2" = list("colr6"), # bad option
                           "ext" = list("placeholder"))
settings <- hierplane_settings(attributes = c("height", "age")) %>%
  add_styles(
    node_type_to_style = node_type_to_style
  )

hierplane(a, title = "Family Tree", settings = settings, theme = "dark")

## example 2
link_to_positions <- list(cousssssin = "top") # bad option
settings <- hierplane_settings(attributes = c("height", "age")) %>%
  add_styles(
    link_to_positions = link_to_positions
  )

hierplane(a, title = "Family Tree", settings = settings, theme = "dark")

# spacy styling demo
node_type_to_style <- list(ROOT = list("color1", "strong"),
                           dobj = list("color6"),
                           nsubj = list("color2"),
                           punct = list("placeholder"))
link_to_positions <- list(nsubj = "left", dobj = "right")
link_name_to_label <- list(nsubj = "subj", dobj = "obj")
settings <- spacy_default() %>%
  add_styles(node_type_to_style = node_type_to_style,
             link_to_positions = link_to_positions,
             link_name_to_label = link_name_to_label)

hierplane_spacy("Bob has three daughters and a grandson.",
                settings = settings, theme = "dark")

# bad nesting that is fixed automatically in format_style
node_type_to_style <- list(ROOT = c("color1", "strong"),
                           dobj = list("color6"),
                           nsubj = "color2",
                           punct = "placeholder")
settings <- spacy_default() %>%
  add_styles(node_type_to_style = node_type_to_style,
             link_to_positions = link_to_positions,
             link_name_to_label = link_name_to_label)

hierplane_spacy("Bob has three daughters and a grandson.",
                settings = settings, theme = "dark")
tylerlittlefield commented 4 years ago

All tests have passed, merging! Thank you! 🎉