-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
163 lines (127 loc) · 3.76 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# hierplane
<!-- badges: start -->
[![R build status](https://github.com/tyluRp/hierplane/workflows/R-CMD-check/badge.svg)](https://github.com/tyluRp/hierplane/actions)
[![Codecov test coverage](https://codecov.io/gh/tyluRp/hierplane/branch/master/graph/badge.svg)](https://codecov.io/gh/tyluRp/hierplane?branch=master)
<!-- badges: end -->
:warning: Work in progress :warning:
The goal of `hierplane` is to visualize trees. This is an HTML widget that uses source code from the [original javascript library](https://github.com/allenai/hierplane). A handful of functions are provided that allow R users to render hierplanes in shiny. See a live demonstration [here](https://tylerlittlefield.com/shiny/tyler/hierplane/).
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("tyluRp/hierplane")
```
## Example
Rendering a hierplane requires you to:
1. Create a hierplane object with `hp_` functions
2. Render the hierplane with `hierplane()`
A hierplane object can be created from different input data, below are some examples:
```{r creating-hierplain-objects, message=FALSE}
library(hierplane)
# from a string, requires spacyr package
hp_spacyr("Sam likes boats")
# from a dataframe
hp_dataframe(starships)
# from YAML, requires data.tree and yaml package
yaml <- "
name: r4fun
tyler:
name: Tyler
job: Data Scientist
species: Human
"
x <- data.tree::as.Node(yaml::yaml.load(yaml))
hp_datatree(x)
```
With this, we can render a hierplane in shiny:
```r
library(hierplane)
library(shiny)
ui <- fluidPage(
hierplaneOutput("hplane")
)
server <- function(input, output, session) {
output$hplane <- renderHierplane({
x <- hp_spacyr("Sam likes boats")
hierplane(x)
})
}
shinyApp(ui, server)
```
```{r render-screenshot, echo=FALSE, dpi=300}
knitr::include_graphics("man/figures/hierplane_spacyr.png")
```
If you are familiar with `data.tree`, you can use it to visualize trees as well. Here is an example of generating a hierplane from YAML:
```r
library(data.tree)
library(yaml)
"
name: r4fun
tyler:
name: Tyler
job: Data Scientist
species: Human
toulouse:
name: Toulouse
job: Systems Engineer
species: Cat
jojo:
name: Jojo
job: Python Programmer
species: Dog
ollie:
name: Ollie
job: Database Administrator
species: Dog
lucas:
name: Lucas
job: R Programmer
species: Rabbit
" -> yaml
yaml %>%
yaml.load() %>%
as.Node() %>%
hp_datatree(
title = "r4fun github group",
link = "species",
attributes = "job"
) %>%
hierplane(
theme = "light",
width = "auto",
height = "auto"
)
```
```{r, echo=FALSE, dpi=300}
knitr::include_graphics("man/figures/hierplane_datatree.png")
```
While hierarchical data isn't common in a `data.frame` centric language like R, we have developed a way to parse a `data.frame` to hierplane ready data. This works by using `hp_dataframe()`:
```r
ui <- fluidPage(
hierplaneOutput("hplane")
)
server <- function(input, output, session) {
output$hplane <- renderHierplane({
hierplane(hp_dataframe(starships, title = "Starships"))
})
}
shinyApp(ui, server)
```
```{r render-screenshot2, echo=FALSE, dpi=300}
knitr::include_graphics("man/figures/hierplane_dataframe.png")
```
## Acknowledgements
* [`allenai/hierplane`](https://github.com/allenai/hierplane): The original javascript library that this package uses
* [`DeNeutoy/spacy-vis`](https://github.com/DeNeutoy/spacy-vis): Spacy models using hierplane