Visualizing dm objects


Once you have all your primary keys set and all foreign key relations defined, a graphical representation of your data model offers a condensed view of the tables and the relationships between the tables. The following functions can be used to visualize the dm object:1

  1. dm_draw()
  2. dm_set_colors()
  3. dm_get_colors()
  4. dm_get_available_colors()

We use the prepared example dm object dm_nycflights13(cycle = TRUE):

flights_dm_w_many_keys <- dm_nycflights13(cycle = TRUE, color = FALSE)

The schema is drawn with dm_draw().


You can use colors to visually group your tables into families to reflect their logical grouping. The available colors are either hexcoded colors or the standard R color names. The function dm_get_available_colors() forwards to grDevices::colors():


Colors are assigned with dm_set_colors() using syntax known in the {tidyverse} as {tidyselect}-syntax, here in the form: color = table. Select helper functions are supported. The result of dm_set_colors() is a dm object. The information about the color is stored together with the rest of the metadata.

flights_dm_w_many_keys_and_colors <-
  flights_dm_w_many_keys %>%
    maroon4 = flights,
    orange = starts_with("air"),
    "#5986C4" = planes

Draw the schema with dm_draw().


The colors can be queried with dm_get_colors().


See the documentation for dm_draw() for further options. One important argument is view_type. Besides the default "keys_only", it accepts "all" to display all columns, and "title_only" to show only the title of the table.

flights_dm_w_many_keys_and_colors %>%
  dm_draw(view_type = "title_only")

If you would like to visualize only some of the tables, use dm_select_tbl() before drawing:

flights_dm_w_many_keys_and_colors %>%
  dm_select_tbl(flights, airports, planes) %>%

Finally, for exporting a drawing to svg you could use DiagrammeRsvg::export_svg():

flights_dm_w_many_keys_and_colors %>%
  dm_select_tbl(flights, airports, planes) %>%
  dm_draw() %>%
  DiagrammeRsvg::export_svg() %>%

  1. The code for the functions in this section is borrowed from the {datamodelr} package.↩︎