Skip to Content

use datamodelr to create database schema diagrams

Thanks to R for Data Science Solutions for telling me how to draw database schema diagrams.

Here we use the datamodelr package: 1

# install needed R packages
remotes::update_packages(c('magrittr', 'Lahman', 'DiagrammeR'), upgrade = TRUE)
remotes::install_github(c("bergant/datamodelr"))

Following is a simple example:

library(magrittr)
library(datamodelr)

list(Batting = Lahman::Batting, Master = Lahman::Master, Salaries = Lahman::Salaries) %>% 
    dm_from_data_frames() %>%
    dm_set_key("Batting", c("playerID", "yearID", "stint")) %>%
    dm_set_key("Master", "playerID") %>%
    dm_set_key("Salaries", c("yearID", "teamID", "playerID")) %>%
    dm_add_references(
        Batting$playerID == Master$playerID, 
        Salaries$playerID == Master$playerID
    ) %>% 
    dm_create_graph(rankdir = "LR", columnArrows = TRUE) %>% 
    dm_render_graph()

You can see more examples in the package’s GitHub homepage.


  1. As of this writing, the package use Suggests to list dependencies, I have lanched an issue.

    But for now, we can only use dependencies = T to install “Depends”, “Imports”, “LinkingTo” and “Suggests”.↩︎