dm_from_con()
con <- DBI::dbConnect(...)
dm_from_con(con)
dm(df1, df2, ...)
dm(df1, df2, df3)
dm(dm1, df1...)
dm(dm1, df1)
dm(dm1, dm2)
dm_add_pk()
, dm_add_fk()
Automatic for MariaDB, SQL Server, Postgres, and others.
Identify potential primary keys:
dm_enum_pk_candidates()
: columns, candidate, why.Add primary keys:
dm1 |>
dm_add_pk(table, columns)
Identify potential foreign keys:
dm_enum_fk_candidates()
: columns, candidate, why.Add foreign keys:
dm1 |>
dm_add_fk(table, column)
The dm package provides a grammar of relational data models. It helps maintain referential integrity.
A dm behaves like a list of tables (data frames or lazy tables) capturing relationships between the tables.
Shiny app: dm_gui(dm = dm1)
dm_select_tbl(dm1, ...)
dm1 |>
dm_select_tbl(-df3)
dm_rename_tbl(dm1, ...)
dm_select(dm1, table, ...)
Automatic update of dm meta-information and table relations.
dm1 |>
dm_select(df3, -c3, -c4)
dm_rename(dm1, table, ...)
dm_filter(dm1, table = (pred))
Filter rows in the table where the condition is defined, but also all directly/indirectly connected tables.
dm1 |>
dm_filter(df3 = (x == "val"))
dm_draw()
dm_draw(view_type = "keys_only")
.dm_draw(view_type = "all")
.dm_draw(view_type = "title_only")
.dm |>
dm_draw(
view_type = "title_only",
rankdir = "TB"
)
To visualize fewer tables first use dm_select_tbl()
.
dm_set_colors()
dm |>
dm_set_colors(
pink = flights,
orange = starts_with("air")
) |>
dm_draw()
dm_examine_constraints()
tibble with information about which key constraints are met or violated.
dm_examine_cardinalities()
tibble with information about the cardinality of the foreign keys constraints.
check_key(df, col1)
returns an error if not an unique key.
check_subset(df1, df2)
returns an error if df1
is not a subset of
df2
.
check_set_equality(df1, df2)
returns an error if df1
and df2
are not the
same sets.
dm_disambiguate_cols()
dm_disambiguate_cols(dm1)
ensures that all columns in a
dm have unique names.
dm_flatten_to_tbl()
Only direct neighbours: dm_flatten_to_tbl()
dm1 |>
dm_flatten_to_tbl(
.start = df1
)
All neighbours: dm_flatten_to_tbl(.recursive = TRUE)
dm1 |>
dm_flatten_to_tbl(
.start = df1,
.recursive = TRUE
)
dm_wrap_tbl()
Parent tables are packed — dm_pack_tbl()
.
Child tables are nested — dm_nest_tbl()
.
dm1 |>
dm_wrap_tbl(
root = green_df
)
pull_tbl()
dm1 |>
pull_tbl(
dm1,
green_df,
keyed = TRUE
)
If the dm is zoomed, retrieve zoomed table automatically.
dm1 |>
dm_zoom_to(green_df) |>
pull_tbl()
dm_get_tables(keyed = TRUE)
: dm_tbl <- dm1 |>
dm_get_tables(keyed = TRUE)
new_table1 <- dm_tbl$table1 |>
mutate(...)
dm1 |>
dm_select_tbl(-table1) |>
dm(table1 = new_table1)
dm_zoom_to()
: Zoom on a table.zoomed_dm1 <- dm1 |>
dm_zoom_to(green_df)
mutate()
, etc.).zoomed_dm2 <- zoomed_dm1 |>
mutate(var = thing)
dm_update_zoomed()
(replace) /
dm_insert_zoomed()
dm3 <- zoomed_dm2 |>
dm_update_zoomed()
copy_dm_to()
Need a database connection —
DBI::dbConnect()
.
con <- DBI::dbConnect(...)
# Persistent tables:
persistent_dm <- copy_dm_to(
con,
dm1,
temporary = FALSE
)
DBI::dbDisconnect(con)
Methods:
dm_rows_insert(dm1, dm2)
: adds new rowsdm_rows_update(dm1, dm2)
: changes values in rowsdm_rows_patch(dm1, dm2)
: fills in missing valuesdm_rows_upsert(dm1, dm2)
: adds new or changes rowsdm_rows_delete(dm1, dm2)
: deletes rowsdm1 |>
dm_rows_insert(dm2, in_place = FALSE)
dm1 |>
dm_rows_insert(dm2, in_place = TRUE)
A dm is immutable, except with
in_place = TRUE
.