r4fun / hierplane

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

Add `hp_datatree` for making hierplanes from {data.tree} #45

Closed tylerlittlefield closed 4 years ago

tylerlittlefield commented 4 years ago

This PR adds hp_datatree with the goal of making {hierplane} compatible with {data.tree}. Also updated the README a bit. Some examples below:

Creating a tree the {data.tree} way

library(hierplane)
library(data.tree)

acme <- Node$new("Acme Inc.")
accounting <- acme$AddChild("Accounting")
software <- accounting$AddChild("New Software")
standards <- accounting$AddChild("New Accounting Standards")
research <- acme$AddChild("Research")
newProductLine <- research$AddChild("New Product Line")
newLabs <- research$AddChild("New Labs")
it <- acme$AddChild("IT")
outsource <- it$AddChild("Outsource")
agile <- it$AddChild("Go agile")
goToR <- it$AddChild("Switch to R")

acme %>% 
  hp_datatree() %>% 
  hierplane()

Creating a tree from YAML

library(hierplane)
library(data.tree)
library(yaml)

yaml <- "
name: r4fun
tyler:
  name: Tyler
  job: Data Scientist
  species: Human
  toulouse:
    name: Toulouse
    job: Systems Engineer
    species: Cat
    toulouse:
      name: Toulouse
      job: Systems Engineer
      species: Cat
  ollie:
    name: Ollie
    job: Database Administrator
    species: Dog
  lucas:
    name: Lucas
    job: R Programmer
    species: Rabbit
"

yaml %>% 
  yaml.load() %>% 
  as.Node() %>% 
  hp_datatree() %>% 
  hierplane()

Creating a tree from CSV

library(hierplane)
library(data.tree)

fileName <- system.file("extdata", "useR15.csv", package="data.tree")
useRdf <- read.csv(fileName, stringsAsFactors = FALSE)
useRdf$pathString <- paste("useR", useRdf$session, useRdf$room, useRdf$speaker, sep="|")
useRtree <- as.Node(useRdf, pathDelimiter = "|")

useRtree %>% 
  hp_datatree(
    attributes = "presentation"
  ) %>% 
  hierplane(
    width = "auto",
    height = "auto"
  )

Creating a tree from JSON

library(hierplane)
library(data.tree)
library(jsonlite)

"https://raw.githubusercontent.com/tyluRp/catfacts/master/renv.lock" %>% 
  read_json() %>% 
  as.Node() %>% 
  hp_datatree(
    attributes = c(
      "Version",
      "URL",
      "Repository"
    )
  ) %>% 
  hierplane()