- Notifications
You must be signed in to change notification settings - Fork28
bergant/datamodelr
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project is no longer actively maintained. Please take a look atdm package.
Define and display data model diagrams:
Data model definition
- Define data model manually withYAML
- Extract data model fromR data frames
- Reverse-engineerSQL Server Database
- Reverse-engineerPostgreSQL Database
Rendering
- Define modelsegments
- Displayfocused sub-diagram or
- Hide columns to improve model diagram readability
- Usecolors to emphasize specific tables
- Definegraph direction orothergraphviz attributes
- Displayadditional column attributes
Useshiny to implement interactive model definition andrendering.
devtools::install_github("bergant/datamodelr")
Define a data model in YAML:
# data model segments-segment:&md Master data-segment:&tran Transactions# Tables and columns-table:Personsegment:*mdcolumns:Person ID:{key: yes}Name:E-mail:Street:Street number:City:ZIP:-table:Ordersegment:*trancolumns:Order ID:{key: yes}Customer:{ref: Person}Sales person:{ref: Person}Order date:Requested ship date:Status:-table:Order Linesegment:*trancolumns:Order ID:{key: yes, ref: Order}Line number:{key: yes}Order item:{ref: Item}Quantity:Price:-table:Itemsegment:*mddisplay:accent1columns:Item ID:{key: yes}Item Name:Description:Category:Size:Color:
Create a data model object withdm_read_yaml:
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:
graph<- dm_create_graph(dm,rankdir="BT")dm_render_graph(graph)
Attach flights database(nycflights13 package)and create a data model from data frames:
library("nycflights13")dm_f<- dm_from_data_frames(flights,airlines,weather,airports,planes)
Create plot:
graph<- dm_create_graph(dm_f,rankdir="BT",col_attr= c("column","type"))dm_render_graph(graph)
Add references and primary keys:
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)
This example usesNorthwind sampledatabase andRODBCpackage as an interface to SQL Server.
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 modeldm_northwind<- as.data_model(dm_northwind)
Plot the result:
graph<- dm_create_graph(dm_northwind,rankdir="BT")dm_render_graph(graph)
This example usesDVD Rentalsample database andRPostgreSQLpackage as an interface to PostgreSQL database.
library(RPostgreSQL)#> Loading required package: DBIcon<- dbConnect(dbDriver("PostgreSQL"),dbname="dvdrental",user="postgres")sQuery<- dm_re_query("postgres")dm_dvdrental<- dbGetQuery(con,sQuery) dbDisconnect(con)#> [1] TRUEdm_dvdrental<- as.data_model(dm_dvdrental)
Show model:
graph<- dm_create_graph(dm_dvdrental,rankdir="RL")dm_render_graph(graph)
To focus in on a few tables from your model usefocus attribute indm_create_graph function:
focus<-list(tables= c("customer","payment","rental","inventory","film"))graph<- dm_create_graph(dm_dvdrental,rankdir="RL",focus=focus)dm_render_graph(graph)
To emphasize table relations and hide the "non-key""columns useview_type = "keys_only":
graph<- dm_create_graph(dm_dvdrental,view_type="keys_only",rankdir="RL")dm_render_graph(graph)
Arrange tables in clusters withdm_set_segment function:
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:
graph<- dm_create_graph(dm_dvdrental_seg,rankdir="RL",view_type="keys_only")dm_render_graph(graph)
Userankdir to change the direction of graph:
graph<- dm_create_graph(dm_dvdrental_seg,rankdir="BT",view_type="keys_only")dm_render_graph(graph)
To emphasise tables with colors usedm_set_display function:
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)
Default color scheme includes:
Add your colors withdm_add_colors function:
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)
To change general graph, node or edgegraphvizattributes usegraph_attrs,edge_attrs andnode_attrs argumentswhen creating graph. This example changesgraph background,arrow style (edge attribute) andfont (node attribute):
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)
To include additional column attributes setcol_attr when creating graph:
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)
Trydatamodelr Shiny application:
shiny::runApp(system.file("shiny",package="datamodelr"))
datamodelr depends on:
- DiagrammeR for graph rendering
- yaml for parsing YAML files in R
- RStudioshiny andshinyAce for shiny application demo.
About
Data model diagrams in R
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.














