{"id":14069163,"url":"https://github.com/bergant/datamodelr","last_synced_at":"2025-04-09T21:19:48.329Z","repository":{"id":70447501,"uuid":"43719233","full_name":"bergant/datamodelr","owner":"bergant","description":"Data model diagrams in R","archived":false,"fork":false,"pushed_at":"2021-03-02T16:25:40.000Z","size":2181,"stargazers_count":286,"open_issues_count":16,"forks_count":28,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-09T21:19:26.623Z","etag":null,"topics":["data-structures","model-diagram","r"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bergant.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-05T23:14:45.000Z","updated_at":"2025-03-10T19:10:47.000Z","dependencies_parsed_at":"2023-04-05T00:24:28.667Z","dependency_job_id":null,"html_url":"https://github.com/bergant/datamodelr","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Fdatamodelr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Fdatamodelr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Fdatamodelr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergant%2Fdatamodelr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bergant","download_url":"https://codeload.github.com/bergant/datamodelr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248111977,"owners_count":21049578,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structures","model-diagram","r"],"created_at":"2024-08-13T07:06:40.756Z","updated_at":"2025-04-09T21:19:48.287Z","avatar_url":"https://github.com/bergant.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"\n**This project is no longer actively maintained. Please take a look at [dm](https://krlmlr.github.io/dm/) package.**\n\n# datamodelr\n\nDefine and display data model diagrams:\n\n* __Data model definition__\n    + Define data model manually with [YAML](#model-definition-in-yaml)\n    + Extract data model from [R data frames](#model-diagram-of-interconnected-data-frames)\n    + Reverse-engineer [SQL Server Database](#reverse-engineer-sql-server-database)\n    + Reverse-engineer [PostgreSQL Database](#reverse-engineer-postgresql-database)\n\n* __Rendering__\n    + Define model [segments](#diagram-segments)\n    + Display [focused sub-diagram ](#focused-data-model-diagram) or\n    + [Hide columns](#hide-columns) to improve model diagram readability\n    + Use [colors](#colors) to emphasize specific tables\n    + Define [graph direction](#graph-direction) or \n      other [graphviz attributes](#graphviz-attributes)\n    + Display [additional column attributes](#additional-column-attributes)\n\nUse [shiny](#shiny-application) to implement interactive model definition and \nrendering.\n\n## Installation\n\n\n```r\ndevtools::install_github(\"bergant/datamodelr\")\n```\n\n\n## Usage\n\n\n\n\n### Model Definition in YAML\n\n\u003cimg width=\"30%\" align=\"right\" src=\"img/sample.png\" /\u003e\n\nDefine a data model in YAML:\n\n```yaml\n# data model segments\n\n- segment: \u0026md Master data\n- segment: \u0026tran Transactions\n\n# Tables and columns\n\n- table: Person\n  segment: *md\n  columns:\n    Person ID: {key: yes}\n    Name:\n    E-mail:\n    Street:\n    Street number:\n    City:\n    ZIP:\n\n- table: Order\n  segment: *tran\n  columns:\n    Order ID: {key: yes}\n    Customer: {ref: Person}\n    Sales person: {ref: Person}\n    Order date:\n    Requested ship date:\n    Status:\n\n- table: Order Line\n  segment: *tran\n  columns:\n    Order ID: {key: yes, ref: Order}\n    Line number: {key: yes}\n    Order item: {ref: Item}\n    Quantity:\n    Price:\n\n- table: Item\n  segment: *md\n  display: accent1\n  columns:\n    Item ID: {key: yes}\n    Item Name:\n    Description:\n    Category:\n    Size:\n    Color:\n```\n\nCreate a data model object with `dm_read_yaml`:\n\n\n```r\nlibrary(datamodelr)\nfile_path \u003c- system.file(\"samples/example.yml\", package = \"datamodelr\")\ndm \u003c- dm_read_yaml(file_path)\n```\n\nCreate a graph object to plot the model:\n\n\n```r\ngraph \u003c- dm_create_graph(dm, rankdir = \"BT\")\ndm_render_graph(graph)\n```\n\n### Model Diagram of Interconnected Data Frames\nAttach flights database \n([nycflights13](http://github.com/hadley/nycflights13#nycflights13) package) \nand create a data model from data frames:\n\n```r\nlibrary(\"nycflights13\")\ndm_f \u003c- dm_from_data_frames(flights, airlines, weather, airports, planes)\n```\n\nCreate plot:\n\n```r\ngraph \u003c- dm_create_graph(dm_f, rankdir = \"BT\", col_attr = c(\"column\", \"type\"))\ndm_render_graph(graph)\n```\n\n![](img/flights.png)\n\nAdd references and primary keys:\n\n```r\ndm_f \u003c- dm_add_references(\n  dm_f,\n  \n  flights$carrier == airlines$carrier,\n  flights$origin == airports$faa,\n  flights$dest == airports$faa,\n  flights$tailnum == planes$tailnum,\n  weather$origin == airports$faa\n)\ngraph \u003c- dm_create_graph(dm_f, rankdir = \"BT\", col_attr = c(\"column\", \"type\"))\ndm_render_graph(graph)\n```\n\n![](img/flights_references.png)\n\n### Reverse-engineer SQL Server Database\n\nThis example uses [Northwind](https://northwinddatabase.codeplex.com/) sample\ndatabase and [RODBC](http://CRAN.R-project.org/package=RODBC) \npackage as an interface to SQL Server.\n\n\n```r\nlibrary(RODBC)\ncon \u003c- odbcConnect(dsn = \"NW\")\nsQuery \u003c- dm_re_query(\"sqlserver\")\ndm_northwind \u003c- sqlQuery(con, sQuery, stringsAsFactors = FALSE, errors=TRUE)\nodbcClose(con)\n\n# convert to a data model\ndm_northwind \u003c- as.data_model(dm_northwind)\n```\n\nPlot the result:\n\n\n```r\ngraph \u003c- dm_create_graph(dm_northwind, rankdir = \"BT\")\ndm_render_graph(graph)\n```\n\n![](img/northwind.png)\n\n\n### Reverse-engineer PostgreSQL Database\n\nThis example uses [DVD Rental](http://www.postgresqltutorial.com/postgresql-sample-database/) \nsample database and [RPostgreSQL](https://cran.r-project.org/package=RPostgreSQL) \npackage as an interface to PostgreSQL database. \n\n\n```r\nlibrary(RPostgreSQL)\n#\u003e Loading required package: DBI\ncon \u003c- dbConnect(dbDriver(\"PostgreSQL\"), dbname=\"dvdrental\", user =\"postgres\")\nsQuery \u003c- dm_re_query(\"postgres\")\ndm_dvdrental \u003c- dbGetQuery(con, sQuery) \ndbDisconnect(con)\n#\u003e [1] TRUE\n\ndm_dvdrental \u003c- as.data_model(dm_dvdrental)\n```\n\nShow model:\n\n```r\ngraph \u003c- dm_create_graph(dm_dvdrental, rankdir = \"RL\")\ndm_render_graph(graph)\n```\n\n![](img/dvdrental.png)\n\n### Focused Data Model Diagram\n\nTo focus in on a few tables from your model use `focus` attribute in `dm_create_graph` function:\n\n```r\nfocus \u003c- list(tables = c(\n    \"customer\",\n    \"payment\", \n    \"rental\",\n    \"inventory\",\n    \"film\"\n))\n    \ngraph \u003c- dm_create_graph( dm_dvdrental, rankdir = \"RL\", focus = focus)\ndm_render_graph(graph)\n```\n\n![](img/dvdrental_small.png)\n\n### Hide columns\nTo emphasize table relations and hide the \"non-key\"\" \ncolumns use `view_type = \"keys_only\"`:\n\n\n```r\ngraph \u003c- dm_create_graph(dm_dvdrental, view_type = \"keys_only\", rankdir = \"RL\")\ndm_render_graph(graph)\n```\n\n![](img/dvdrental_keys.png)\n\n### Diagram Segments\nArrange tables in clusters with `dm_set_segment` function: \n\n\n```r\ntable_segments \u003c- list(\n  Transactions = c(\"rental\", \"inventory\", \"payment\"),\n  Party = c(\"customer\", \"staff\", \"address\", \"city\", \"country\", \"store\"),\n  Film = c(\"film\", \"film_actor\", \"actor\", \"language\", \"film_category\", \"category\") )\n\ndm_dvdrental_seg \u003c- dm_set_segment(dm_dvdrental, table_segments)\n```\n\nRender diagram with segments:\n\n```r\ngraph \u003c- dm_create_graph(dm_dvdrental_seg, rankdir = \"RL\", view_type = \"keys_only\")\ndm_render_graph(graph)\n```\n\n![](img/dvdrental_seg.png)\n\n### Graph Direction\nUse `rankdir` to change the direction of graph:\n\n\n```r\ngraph \u003c- dm_create_graph(dm_dvdrental_seg, rankdir = \"BT\", view_type = \"keys_only\")\ndm_render_graph(graph)\n```\n\n![](img/dvdrental_bottom_top.png)\n\n### Colors\nTo emphasise tables with colors use `dm_set_display` function:\n\n\n```r\ndisplay \u003c- list(\n  accent1 = c(\"rental\", \"payment\"),\n  accent2 = c(\"customer\"),\n  accent3 = c(\"staff\", \"store\"),\n  accent4 = c(\"film\", \"actor\") )\n\ndm_dvdrental_col \u003c- dm_set_display(dm_dvdrental_seg, display)\ngraph \u003c- dm_create_graph(dm_dvdrental_col, rankdir = \"BT\", view_type = \"keys_only\")\ndm_render_graph(graph)\n```\n\n![](img/dvdrental_colors.png)\n\n\nDefault color scheme includes: \n\n\n![](img/colors.png)\n\n### Custom Colors\nAdd your colors with `dm_add_colors` function:\n\n```r\nmy_colors \u003c-\n  dm_color_scheme(\n    purple = dm_palette(\n      line_color = \"#8064A2\",\n      header_bgcolor = \"#B1A0C7\",\n      header_font = \"#FFFFFF\",\n      bgcolor = \"#E4DFEC\"\n    ),\n    red = dm_palette(\n      line_color = \"#C0504D\",\n      header_bgcolor = \"#DA9694\",\n      header_font = \"#FFFFFF\",\n      bgcolor = \"#F2DCDB\"\n    )\n)\n\ndm_add_colors(my_colors)\n\ndm \u003c- dm_set_display(dm, display = list(\n  red = c(\"Order\", \"Order Line\"),\n  purple = \"Item\"\n))\n\ngraph \u003c- dm_create_graph(dm, rankdir = \"RL\")\ndm_render_graph(graph)\n\n```\n\n![](img/sample_colors.png)\n\n### Graphviz Attributes\nTo change general graph, node or edge\n[graphviz](http://www.graphviz.org/doc/info/attrs.html) \nattributes use `graph_attrs`, `edge_attrs` and `node_attrs` arguments\nwhen creating graph.  This example changes\ngraph background,\narrow style (edge attribute) and \nfont (node attribute):\n\n\n```r\ngraph \u003c- dm_create_graph( \n  dm, \n  graph_attrs = \"rankdir = RL, bgcolor = '#F4F0EF' \", \n  edge_attrs = \"dir = both, arrowtail = crow, arrowhead = odiamond\",\n  node_attrs = \"fontname = 'Arial'\")\n                          \ndm_render_graph(graph)\n```\n\n![](img/attributes.png)\n\n### Additional Column Attributes\nTo include additional column attributes set `col_attr` when creating graph:\n\n\n```r\nfocus \u003c- list(tables = c(\n    \"customer\",\n    \"rental\",\n    \"inventory\",\n    \"film\"\n))\n    \ngraph \u003c- dm_create_graph( dm_dvdrental, rankdir = \"RL\", focus = focus,\n                          col_attr = c(\"column\", \"type\"))\ndm_render_graph(graph)\n```\n\n![](img/col_attributes.png)\n\n### Shiny Application\nTry **datamodelr** Shiny application:\n\n\n```r\nshiny::runApp(system.file(\"shiny\", package = \"datamodelr\"))\n```\n\n![](img/shiny.png)\n\n\n\n## Utilised Packages \ndatamodelr depends on:\n\n* [DiagrammeR](http://rich-iannone.github.io/DiagrammeR/) for graph rendering\n* [yaml](https://github.com/viking/r-yaml) for parsing YAML files in R\n* RStudio [shiny](http://shiny.rstudio.com/) and\n   [shinyAce](http://trestletech.github.io/shinyAce/) for shiny application demo.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbergant%2Fdatamodelr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbergant%2Fdatamodelr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbergant%2Fdatamodelr/lists"}