Once you have all primary keys set and all foreign key relations defined, a graphical representation of your data model offers a condensed view over the tables and relationships. Relevant functions for visualising the dm object’s data model are:1

  1. cdm_draw()
  2. cdm_set_colors()
  3. cdm_get_colors()
  4. cdm_get_available_colors()

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

#> ── Table source ───────────────────────────────────────────────────────────
#> src:  <package: nycflights13>
#> ── Data model ─────────────────────────────────────────────────────────────
#> Data model object:
#>   5 tables:  airlines, airports, flights, planes ... 
#>   53 columns
#>   3 primary keys
#>   3 references
#> ── Filters ────────────────────────────────────────────────────────────────
#> None

The schema is drawn with cdm_draw().

cdm_draw(flights_dm_w_many_keys)
%0 airlines airlinescarrierairports airportsfaaflights flightscarriertailnumoriginflights:carrier->airlines:carrier flights:origin->airports:faa planes planestailnumflights:tailnum->planes:tailnum weather weather

You can use colors to visually group your tables into families (which could reflect their logical grouping). Currently, there are only eight different colors available (plus optional borders around the tables). To find out which colors exist, use cdm_get_available_colors():

#> # A tibble: 15 x 3
#>    dm            datamodelr nb         
#>    <chr>         <chr>      <chr>      
#>  1 default       default    (border)   
#>  2 blue_nb       accent1nb  (no border)
#>  3 orange_nb     accent2nb  (no border)
#>  4 yellow_nb     accent3nb  (no border)
#>  5 green_nb      accent4nb  (no border)
#>  6 dark_blue_nb  accent5nb  (no border)
#>  7 light_grey_nb accent6nb  (no border)
#>  8 grey_nb       accent7nb  (no border)
#>  9 blue          accent1    (border)   
#> 10 orange        accent2    (border)   
#> 11 yellow        accent3    (border)   
#> 12 green         accent4    (border)   
#> 13 dark_blue     accent5    (border)   
#> 14 light_grey    accent6    (border)   
#> 15 grey          accent7    (border)

The assignment is carried out with cdm_set_colors(), using a syntax similar to switch(). Any table you don’t specifically assign will not be altered. The result of cdm_set_colors() is a dm object. The information about the color is stored in its data_model-part.

Draw the schema with cdm_draw().

cdm_draw(flights_dm_w_many_keys_and_colors)
%0 airlines airlinescarrierairports airportsfaaflights flightscarriertailnumoriginflights:carrier->airlines:carrier flights:origin->airports:faa planes planestailnumflights:tailnum->planes:tailnum weather weather

The colors can be queried with cdm_get_colors().

cdm_get_colors(flights_dm_w_many_keys_and_colors)
#> # A tibble: 5 x 2
#>   table    color   
#>   <chr>    <chr>   
#> 1 airlines orange  
#> 2 airports orange  
#> 3 flights  blue    
#> 4 planes   green_nb
#> 5 weather  NA

See the documentation for cdm_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.

%0 airlines airlinesairports airportsflights flightsflights:carrier->airlines:carrier flights:origin->airports:faa planes planesflights:tailnum->planes:tailnum weather weather

In case you would like to only see a subset of the whole data model, use cdm_select_tbl() before drawing:

%0 airports airportsfaaflights flightstailnumoriginflights:origin->airports:faa planes planestailnumflights:tailnum->planes:tailnum

  1. The functions in this section rely heavily on related functions of {datamodelr}.