-
Notifications
You must be signed in to change notification settings - Fork 28
/
readme.rmd
465 lines (368 loc) · 10.3 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
---
output:
html_document:
keep_md: yes
self_contained: no
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "img/README-"
)
eval_render <- FALSE # turn on/off SVG rendering
```
# datamodelr
Define and display data model diagrams:
* __Data model definition__
+ Define data model manually with [YAML](#model-definition-in-yaml)
+ Extract data model from [R data frames](#model-diagram-of-interconnected-data-frames)
+ Reverse-engineer [SQL Server Database](#reverse-engineer-sql-server-database)
+ Reverse-engineer [PostgreSQL Database](#reverse-engineer-postgresql-database)
* __Rendering__
+ Define model [segments](#diagram-segments)
+ Display [focused sub-diagram ](#focused-data-model-diagram) or
+ [Hide columns](#hide-columns) to improve model diagram readability
+ Use [colors](#colors) to emphasize specific tables
+ Define [graph direction](#graph-direction) or
other [graphviz attributes](#graphviz-attributes)
+ Display [additional column attributes](#additional-column-attributes)
Use [shiny](#shiny-application) to implement interactive model definition and
rendering.
## Installation
```{r eval=FALSE}
devtools::install_github("bergant/datamodelr")
```
## Usage
### Model Definition in YAML
<img width="30%" align="right" src="img/sample.png" />
Define a data model in YAML:
```yaml
# data model segments
- segment: &md Master data
- segment: &tran Transactions
# Tables and columns
- table: Person
segment: *md
columns:
Person ID: {key: yes}
Name:
E-mail:
Street:
Street number:
City:
ZIP:
- table: Order
segment: *tran
columns:
Order ID: {key: yes}
Customer: {ref: Person}
Sales person: {ref: Person}
Order date:
Requested ship date:
Status:
- table: Order Line
segment: *tran
columns:
Order ID: {key: yes, ref: Order}
Line number: {key: yes}
Order item: {ref: Item}
Quantity:
Price:
- table: Item
segment: *md
display: accent1
columns:
Item ID: {key: yes}
Item Name:
Description:
Category:
Size:
Color:
```
Create a data model object with `dm_read_yaml`:
```{r sample}
library(datamodelr)
file_path <- system.file("samples/example.yml", package = "datamodelr")
dm <- dm_read_yaml(file_path)
```
Create a graph object to plot the model:
```{r sample_render, eval=eval_render, fig.height=8}
graph <- dm_create_graph(dm, rankdir = "BT")
dm_render_graph(graph)
```
### Model Diagram of Interconnected Data Frames
Attach flights database
([nycflights13](http://github.com/hadley/nycflights13#nycflights13) package)
and create a data model from data frames:
```{r flights }
#install.packages("nycflights13")
library("nycflights13")
dm_f <- dm_from_data_frames(flights, airlines, weather, airports, planes)
```
Create plot:
```{r flights_render, eval=eval_render}
graph <- dm_create_graph(dm_f, rankdir = "BT", col_attr = c("column", "type"))
dm_render_graph(graph)
```
![](img/flights.png)
Add references and primary keys:
```{r flights_render_ref, eval=eval_render, fig.height=7}
dm_f <- dm_add_references(
dm_f,
flights$carrier == airlines$carrier,
flights$origin == airports$faa,
flights$dest == airports$faa,
flights$tailnum == planes$tailnum,
weather$origin == airports$faa
)
graph <- dm_create_graph(dm_f, rankdir = "BT", col_attr = c("column", "type"))
dm_render_graph(graph)
```
![](img/flights_references.png)
### Reverse-engineer SQL Server Database
This example uses [Northwind](https://northwinddatabase.codeplex.com/) sample
database and [RODBC](http://CRAN.R-project.org/package=RODBC)
package as an interface to SQL Server.
```{r sql}
library(RODBC)
con <- odbcConnect(dsn = "NW")
sQuery <- dm_re_query("sqlserver")
dm_northwind <- sqlQuery(con, sQuery, stringsAsFactors = FALSE, errors=TRUE)
odbcClose(con)
# convert to a data model
dm_northwind <- as.data_model(dm_northwind)
```
Plot the result:
```{r sql_render, eval=eval_render}
graph <- dm_create_graph(dm_northwind, rankdir = "BT")
dm_render_graph(graph)
```
![](img/northwind.png)
### Reverse-engineer PostgreSQL Database
This example uses [DVD Rental](http://www.postgresqltutorial.com/postgresql-sample-database/)
sample database and [RPostgreSQL](https://cran.r-project.org/package=RPostgreSQL)
package as an interface to PostgreSQL database.
```{r postgres, fig.height=9}
library(RPostgreSQL)
con <- dbConnect(dbDriver("PostgreSQL"), dbname="dvdrental", user ="postgres")
sQuery <- dm_re_query("postgres")
dm_dvdrental <- dbGetQuery(con, sQuery)
dbDisconnect(con)
dm_dvdrental <- as.data_model(dm_dvdrental)
```
Show model:
```{r postgres_render, eval=eval_render}
graph <- dm_create_graph(dm_dvdrental, rankdir = "RL")
dm_render_graph(graph)
```
![](img/dvdrental.png)
### Focused Data Model Diagram
To focus in on a few tables from your model use `focus` attribute in `dm_create_graph` function:
```{r posgres_focus_render, eval=eval_render}
focus <- list(tables = c(
"customer",
"payment",
"rental",
"inventory",
"film"
))
graph <- dm_create_graph( dm_dvdrental, rankdir = "RL", focus = focus)
dm_render_graph(graph)
```
![](img/dvdrental_small.png)
### Hide columns
To emphasize table relations and hide the "non-key""
columns use `view_type = "keys_only"`:
```{r postgres_keys_render, eval=eval_render}
graph <- dm_create_graph(dm_dvdrental, view_type = "keys_only", rankdir = "RL")
dm_render_graph(graph)
```
![](img/dvdrental_keys.png)
### Diagram Segments
Arrange tables in clusters with `dm_set_segment` function:
```{r postgres_segments}
table_segments <- list(
Transactions = c("rental", "inventory", "payment"),
Party = c("customer", "staff", "address", "city", "country", "store"),
Film = c("film", "film_actor", "actor", "language", "film_category", "category") )
dm_dvdrental_seg <- dm_set_segment(dm_dvdrental, table_segments)
```
Render diagram with segments:
```{r postgres_segments_render, eval=eval_render, fig.height=6}
graph <- dm_create_graph(dm_dvdrental_seg, rankdir = "RL", view_type = "keys_only")
dm_render_graph(graph)
```
![](img/dvdrental_seg.png)
### Graph Direction
Use `rankdir` to change the direction of graph:
```{r postgres_dir_render, eval=eval_render}
graph <- dm_create_graph(dm_dvdrental_seg, rankdir = "BT", view_type = "keys_only")
dm_render_graph(graph)
```
![](img/dvdrental_bottom_top.png)
### Colors
To emphasise tables with colors use `dm_set_display` function:
```{r colors_render, eval=eval_render, fig.height=7}
display <- list(
accent1 = c("rental", "payment"),
accent2 = c("customer"),
accent3 = c("staff", "store"),
accent4 = c("film", "actor") )
dm_dvdrental_col <- dm_set_display(dm_dvdrental_seg, display)
graph <- dm_create_graph(dm_dvdrental_col, rankdir = "BT", view_type = "keys_only")
dm_render_graph(graph)
```
![](img/dvdrental_colors.png)
Default color scheme includes:
```{r color_scheme, echo=FALSE, fig.height=1, eval=eval_render}
dm1 <- dm_read_yaml(text = "
- table: accent1
display: accent1
columns:
column 01:
column 02:
- table: accent2
display: accent2
columns:
column 01:
column 02:
- table: accent3
display: accent3
columns:
column 01:
column 02:
- table: accent4
display: accent4
columns:
column 01:
column 02:
- table: accent5
display: accent5
columns:
column 01:
column 02:
- table: accent6
display: accent6
columns:
column 01:
column 02:
- table: accent7
display: accent7
columns:
column 01:
column 02:
")
g1 <- dm_create_graph(dm1)
dm_render_graph(g1)
dm1 <- dm_read_yaml(text = "
- table: accent1nb
display: accent1nb
columns:
column 01:
column 02:
- table: accent2nb
display: accent2nb
columns:
column 01:
column 02:
- table: accent3nb
display: accent3nb
columns:
column 01:
column 02:
- table: accent4nb
display: accent4nb
columns:
column 01:
column 02:
- table: accent5nb
display: accent5nb
columns:
column 01:
column 02:
- table: accent6nb
display: accent6nb
columns:
column 01:
column 02:
- table: accent7nb
display: accent7nb
columns:
column 01:
column 02:
")
g1 <- dm_create_graph(dm1)
dm_render_graph(g1)
```
![](img/colors.png)
### Custom Colors
Add your colors with `dm_add_colors` function:
```{r custom_colors, eval=eval_render, fig.height=4}
my_colors <-
dm_color_scheme(
purple = dm_palette(
line_color = "#8064A2",
header_bgcolor = "#B1A0C7",
header_font = "#FFFFFF",
bgcolor = "#E4DFEC"
),
red = dm_palette(
line_color = "#C0504D",
header_bgcolor = "#DA9694",
header_font = "#FFFFFF",
bgcolor = "#F2DCDB"
)
)
dm_add_colors(my_colors)
dm <- dm_set_display(dm, display = list(
red = c("Order", "Order Line"),
purple = "Item"
))
graph <- dm_create_graph(dm, rankdir = "RL")
dm_render_graph(graph)
```
![](img/sample_colors.png)
### Graphviz Attributes
To change general graph, node or edge
[graphviz](http://www.graphviz.org/doc/info/attrs.html)
attributes use `graph_attrs`, `edge_attrs` and `node_attrs` arguments
when creating graph. This example changes
graph background,
arrow style (edge attribute) and
font (node attribute):
```{r dot_attributes, eval=eval_render, fig.height=4}
graph <- dm_create_graph(
dm,
graph_attrs = "rankdir = RL, bgcolor = '#F4F0EF' ",
edge_attrs = "dir = both, arrowtail = crow, arrowhead = odiamond",
node_attrs = "fontname = 'Arial'")
dm_render_graph(graph)
```
![](img/attributes.png)
### Additional Column Attributes
To include additional column attributes set `col_attr` when creating graph:
```{r col_attr, eval=eval_render}
focus <- list(tables = c(
"customer",
"rental",
"inventory",
"film"
))
graph <- dm_create_graph( dm_dvdrental, rankdir = "RL", focus = focus,
col_attr = c("column", "type"))
dm_render_graph(graph)
```
![](img/col_attributes.png)
### Shiny Application
Try **datamodelr** Shiny application:
```{r eval=FALSE}
shiny::runApp(system.file("shiny", package = "datamodelr"))
```
![](img/shiny.png)
## Utilised Packages
datamodelr depends on:
* [DiagrammeR](http://rich-iannone.github.io/DiagrammeR/) for graph rendering
* [yaml](https://github.com/viking/r-yaml) for parsing YAML files in R
* RStudio [shiny](http://shiny.rstudio.com/) and
[shinyAce](http://trestletech.github.io/shinyAce/) for shiny application demo.