r4fun / hierplane

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

Work on hp_dataframe documentation #38

Open tylerlittlefield opened 4 years ago

tylerlittlefield commented 4 years ago

Creating an hp_dataframe object isn't easy, but I think it can be. It just doesn't seem very intuitive to me at the moment. I have worked on a small shiny app for explaining a hp_dataframe object in a way that makes it a little clearer. Let's try and brainstorm some common workflows for taking an existing dataset, whatever it is and converting it to an hp_dataframe. Maybe a set of steps where the first one would be a strategy for recognizing if your dataset is even compatible with hierplain, e.g. if it has potential to be represented as a tree in the first place. I also have a feeling that the use of case_when() or gathering data from wider to long might be common workflows.

library(hierplane)
library(reactable)
library(magrittr)
library(shiny)

df <- data.frame(
  # mapping 
  parent_id = c(1, 1, 1, 2),
  child_id = c(1, 2, 3, 4),
  # boxes
  child = c("root", "child 1", "child 2", "child 1a"),
  # links connecting the boxes
  link = c("ROOT", "child 1 link", "child 2 link", "child 1a link"),
  # controls the color of each box, if every value is the same 1 color
  node_type = c("root color", "child 1 color", "child 2 color", "child 1a color"),
  # attributes
  attribute1 = c("root attr 1", "child 1 attr", "child 2 attr", "child 1a attr"),
  attribute2 = c("root attr 2", "child 1 attr 2", "child 2 attr 2", "child 1a attr 2")
)

ui <- fluidPage(
  hierplaneOutput("hplane", height = "100%"),
  reactableOutput("hplane_tbl")
)

server <- function(input, output, session) {
  output$hplane <- renderHierplane({
    df %>% 
      hp_dataframe(title = "Anatomy of an hp_dataframe object") %>% 
      hierplane()
  })

  output$hplane_tbl <- renderReactable({
    reactable(df)
  })
}

shinyApp(ui, server)
mathidachuk commented 4 years ago

I think the most common workflow would be a loop. And transforming data to long will not necessarily get what you want. The main issue with this is that you will lose the linkage between each plane.

I think the dataframe you have put together is a very good demo! Also I love how you call it "anatomy", which speaks to the complex nature of hierarchical data.

If I had to break down a dataframe for hierplane, there are 3 main components:

  1. ROOT - This is the base node. Everything stems from this. Most likely this is not part of the input data. It likely links several top level items together.
  2. Top level - This is the first layer and is directly linked to node. Most likely this is the top level of the hierarchical data and is manually linked to the ROOT. Bind rows to ROOT.
  3. All other levels - These levels stem from the top level. Each layer can be created by taking two related columns, along with the desired linkage (if any) and attributes (if any), removing duplicates and NAs, then appending to the ROOT and top level components.

I am working on a more general example (simpler than starships lol) that will help demonstrate the thought process behind constructing a hierplane dataset (or hierarchical dataset in general). Maybe we will get some generalized data building functions out of it! Will keep you posted.