The dm class holds a list of tables and their relationships. It is inspired by datamodelr, and extends the idea by offering operations to access the data in the tables.

dm() creates a dm object from tbl objects (tibbles or lazy data objects).

new_dm() is a low-level constructor that creates a new dm object.

  • If called without arguments, it will create an empty dm.

  • If called with arguments, no validation checks will be made to ascertain that the inputs are of the expected class and internally consistent; use validate_dm() to double-check the returned object.

validate_dm() checks the internal consistency of a dm object.

dm_get_src() returns the dplyr source for a dm object. All tables in a dm object must be from the same source, i.e. either they are all data frames, or they all are stored on the same database.

dm_get_con() returns the DBI::DBIConnection for dm objects. This works only if the tables are stored on a database, otherwise an error is thrown.

dm_get_tables() returns a named list of dplyr tbl objects of a dm object. Filtering expressions are NOT evaluated at this stage. To get a filtered table, use dm_apply_filters_to_tbl(), to apply filters to all tables use dm_apply_filters()

is_dm() returns TRUE if the input is of class dm.

as_dm() coerces objects to the dm class

dm(..., .name_repair = c("check_unique", "unique", "universal", "minimal"))

new_dm(tables = list())

validate_dm(x)

dm_get_src(x)

dm_get_con(x)

dm_get_tables(x)

is_dm(x)

as_dm(x)

Arguments

...

Tables to add to the dm object. If no names are provided, the tables are auto-named.

.name_repair

Options for name repair. Forwarded as repair to vctrs::vec_as_names().

tables

A named list of the tables (tibble-objects, not names), to be included in the dm object.

x

An object.

Value

For dm(), new_dm(), as_dm(): A dm object.

For validate_dm(): Returns the dm, invisibly, after finishing all checks.

For dm_get_src(): the dplyr source for a dm object.

For dm_get_con(): The DBI::DBIConnection for dm objects.

For dm_get_tables(): A named list with the tables constituting the dm.

For is_dm(): Boolean, is this object a dm.

See also

Examples

dm(iris, mtcars)
#> ── Table source ──────────────────────────────────────────────────────────────── #> src: <environment: R_GlobalEnv> #> ── Metadata ──────────────────────────────────────────────────────────────────── #> Tables: `iris`, `mtcars` #> Columns: 16 #> Primary keys: 0 #> Foreign keys: 0
new_dm(list(iris = iris, mtcars = mtcars))
#> ── Table source ──────────────────────────────────────────────────────────────── #> src: <environment: R_GlobalEnv> #> ── Metadata ──────────────────────────────────────────────────────────────────── #> Tables: `iris`, `mtcars` #> Columns: 16 #> Primary keys: 0 #> Foreign keys: 0
as_dm(list(iris = iris, mtcars = mtcars))
#> ── Table source ──────────────────────────────────────────────────────────────── #> src: <environment: R_GlobalEnv> #> ── Metadata ──────────────────────────────────────────────────────────────────── #> Tables: `iris`, `mtcars` #> Columns: 16 #> Primary keys: 0 #> Foreign keys: 0
dm_nycflights13() %>% tbl("airports")
#> # A tibble: 1,458 x 8 #> faa name lat lon alt tz dst tzone #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/New_Yo… #> 2 06A Moton Field Municipal A… 32.5 -85.7 264 -6 A America/Chicago #> 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/Chicago #> 4 06N Randall Airport 41.4 -74.4 523 -5 A America/New_Yo… #> 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/New_Yo… #> 6 0A9 Elizabethton Municipal … 36.4 -82.2 1593 -5 A America/New_Yo… #> 7 0G6 Williams County Airport 41.5 -84.5 730 -5 A America/New_Yo… #> 8 0G7 Finger Lakes Regional A… 42.9 -76.8 492 -5 A America/New_Yo… #> 9 0P2 Shoestring Aviation Air… 39.8 -76.6 1000 -5 U America/New_Yo… #> 10 0S9 Jefferson County Intl 48.1 -123. 108 -8 A America/Los_An… #> # … with 1,448 more rows
#> [1] "airlines" "airports" "flights" "planes" "weather"
dm_nycflights13() %>% dm_get_src()
#> src: <environment: R_GlobalEnv> #> tbls:
copy_dm_to( dbplyr::src_memdb(), dm_nycflights13(), unique_table_names = TRUE ) %>% dm_get_con()
#> Warning: The `unique_table_names` argument of `copy_dm_to()` is deprecated as of lifecycle 0.1.4. #> Use `table_names = identity` to use unchanged names for temporary tables. #> This warning is displayed once every 8 hours. #> Call `lifecycle::last_warnings()` to see where this warning was generated.
#> <SQLiteConnection> #> Path: :memory: #> Extensions: TRUE
dm_nycflights13() %>% dm_get_tables()
#> $airlines #> # A tibble: 16 x 2 #> carrier name #> <chr> <chr> #> 1 9E Endeavor Air Inc. #> 2 AA American Airlines Inc. #> 3 AS Alaska Airlines Inc. #> 4 B6 JetBlue Airways #> 5 DL Delta Air Lines Inc. #> 6 EV ExpressJet Airlines Inc. #> 7 F9 Frontier Airlines Inc. #> 8 FL AirTran Airways Corporation #> 9 HA Hawaiian Airlines Inc. #> 10 MQ Envoy Air #> 11 OO SkyWest Airlines Inc. #> 12 UA United Air Lines Inc. #> 13 US US Airways Inc. #> 14 VX Virgin America #> 15 WN Southwest Airlines Co. #> 16 YV Mesa Airlines Inc. #> #> $airports #> # A tibble: 1,458 x 8 #> faa name lat lon alt tz dst tzone #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/New_Yo… #> 2 06A Moton Field Municipal A… 32.5 -85.7 264 -6 A America/Chicago #> 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/Chicago #> 4 06N Randall Airport 41.4 -74.4 523 -5 A America/New_Yo… #> 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/New_Yo… #> 6 0A9 Elizabethton Municipal … 36.4 -82.2 1593 -5 A America/New_Yo… #> 7 0G6 Williams County Airport 41.5 -84.5 730 -5 A America/New_Yo… #> 8 0G7 Finger Lakes Regional A… 42.9 -76.8 492 -5 A America/New_Yo… #> 9 0P2 Shoestring Aviation Air… 39.8 -76.6 1000 -5 U America/New_Yo… #> 10 0S9 Jefferson County Intl 48.1 -123. 108 -8 A America/Los_An… #> # … with 1,448 more rows #> #> $flights #> # A tibble: 11,227 x 19 #> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time #> <int> <int> <int> <int> <int> <dbl> <int> <int> #> 1 2013 1 10 3 2359 4 426 437 #> 2 2013 1 10 16 2359 17 447 444 #> 3 2013 1 10 450 500 -10 634 648 #> 4 2013 1 10 520 525 -5 813 820 #> 5 2013 1 10 530 530 0 824 829 #> 6 2013 1 10 531 540 -9 832 850 #> 7 2013 1 10 535 540 -5 1015 1017 #> 8 2013 1 10 546 600 -14 645 709 #> 9 2013 1 10 549 600 -11 652 724 #> 10 2013 1 10 550 600 -10 649 703 #> # … with 11,217 more rows, and 11 more variables: arr_delay <dbl>, #> # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>, #> # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm> #> #> $planes #> # A tibble: 3,322 x 9 #> tailnum year type manufacturer model engines seats speed engine #> <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr> #> 1 N10156 2004 Fixed wing m… EMBRAER EMB-1… 2 55 NA Turbo-… #> 2 N102UW 1998 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 3 N103US 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 4 N104UW 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 5 N10575 2002 Fixed wing m… EMBRAER EMB-1… 2 55 NA Turbo-… #> 6 N105UW 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 7 N107US 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 8 N108UW 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 9 N109UW 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> 10 N110UW 1999 Fixed wing m… AIRBUS INDUST… A320-… 2 182 NA Turbo-… #> # … with 3,312 more rows #> #> $weather #> # A tibble: 861 x 15 #> origin year month day hour temp dewp humid wind_dir wind_speed #> <chr> <int> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 EWR 2013 1 10 0 41 32 70.1 230 8.06 #> 2 EWR 2013 1 10 1 39.0 30.0 69.9 210 9.21 #> 3 EWR 2013 1 10 2 39.0 28.9 66.8 230 6.90 #> 4 EWR 2013 1 10 3 39.9 27.0 59.5 270 5.75 #> 5 EWR 2013 1 10 4 41 26.1 55.0 320 6.90 #> 6 EWR 2013 1 10 5 41 26.1 55.0 300 12.7 #> 7 EWR 2013 1 10 6 39.9 25.0 54.8 280 6.90 #> 8 EWR 2013 1 10 7 41 25.0 52.6 330 6.90 #> 9 EWR 2013 1 10 8 43.0 25.0 48.7 330 8.06 #> 10 EWR 2013 1 10 9 45.0 23 41.6 320 17.3 #> # … with 851 more rows, and 5 more variables: wind_gust <dbl>, precip <dbl>, #> # pressure <dbl>, visib <dbl>, time_hour <dttm> #>
#> # A tibble: 0 x 3 #> # … with 3 variables: table <chr>, filter <list>, zoomed <lgl>
dm_nycflights13() %>% validate_dm() is_dm(dm_nycflights13())
#> [1] TRUE
dm_nycflights13()["airports"]
#> ── Table source ──────────────────────────────────────────────────────────────── #> src: <environment: R_GlobalEnv> #> ── Metadata ──────────────────────────────────────────────────────────────────── #> Tables: `airports` #> Columns: 8 #> Primary keys: 1 #> Foreign keys: 0
dm_nycflights13()[["airports"]]
#> # A tibble: 1,458 x 8 #> faa name lat lon alt tz dst tzone #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/New_Yo… #> 2 06A Moton Field Municipal A… 32.5 -85.7 264 -6 A America/Chicago #> 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/Chicago #> 4 06N Randall Airport 41.4 -74.4 523 -5 A America/New_Yo… #> 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/New_Yo… #> 6 0A9 Elizabethton Municipal … 36.4 -82.2 1593 -5 A America/New_Yo… #> 7 0G6 Williams County Airport 41.5 -84.5 730 -5 A America/New_Yo… #> 8 0G7 Finger Lakes Regional A… 42.9 -76.8 492 -5 A America/New_Yo… #> 9 0P2 Shoestring Aviation Air… 39.8 -76.6 1000 -5 U America/New_Yo… #> 10 0S9 Jefferson County Intl 48.1 -123. 108 -8 A America/Los_An… #> # … with 1,448 more rows
dm_nycflights13()$airports
#> # A tibble: 1,458 x 8 #> faa name lat lon alt tz dst tzone #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/New_Yo… #> 2 06A Moton Field Municipal A… 32.5 -85.7 264 -6 A America/Chicago #> 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/Chicago #> 4 06N Randall Airport 41.4 -74.4 523 -5 A America/New_Yo… #> 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/New_Yo… #> 6 0A9 Elizabethton Municipal … 36.4 -82.2 1593 -5 A America/New_Yo… #> 7 0G6 Williams County Airport 41.5 -84.5 730 -5 A America/New_Yo… #> 8 0G7 Finger Lakes Regional A… 42.9 -76.8 492 -5 A America/New_Yo… #> 9 0P2 Shoestring Aviation Air… 39.8 -76.6 1000 -5 U America/New_Yo… #> 10 0S9 Jefferson County Intl 48.1 -123. 108 -8 A America/Los_An… #> # … with 1,448 more rows