## ----setup-------------------------------------------------------------------- library(autodb) ## ----check_diagrammer--------------------------------------------------------- if (requireNamespace("DiagrammeR", quietly = TRUE)) { show <- function(x) DiagrammeR::grViz(gv(x), width = "100%") maybe_plot <- function(x) DiagrammeR::grViz(gv(x), width = "100%") }else{ show <- print maybe_plot <- function(x) invisible(NULL) } ## ----bcnf--------------------------------------------------------------------- fds <- functional_dependency( list(list(c("a", "b"), "c"), list("c", "a")), letters[1:3] ) fds schema <- normalise(fds) ## ----------------------------------------------------------------------------- show(schema) ## ----bcnf2-------------------------------------------------------------------- db <- schema |> create() |> insert(data.frame(c = 1:2, a = 1:2), relations = "c") |> insert(data.frame(a = 1:2, b = 1L, c = 2:1), relations = "a_b") rab <- records(db)$a_b rc <- records(db)$c knitr::kable(rab) knitr::kable(rc) knitr::kable(merge(rab, rc, by = "c", suffixes = c(".a_b", ".c"))) ## ----bcnf3, echo=FALSE-------------------------------------------------------- schema2 <- schema keys(schema2)$a_b <- list(c("b", "c")) attrs(schema2)$a_b <- c("b", "c") names(schema2)[names(schema2) == "a_b"] <- "b_c" ## ----------------------------------------------------------------------------- show(schema2) ## ----nonconnected------------------------------------------------------------- fds <- functional_dependency( list( list("a", "b"), list("a", "c"), list("b", "d"), list("c", "e"), list(c("d", "e"), "f") ), letters[1:6] ) fds ## ----------------------------------------------------------------------------- show(normalise(fds)) ## ----echo=FALSE--------------------------------------------------------------- if (requireNamespace("DiagrammeR", quietly = TRUE)) DiagrammeR::grViz( 'digraph { rankdir = "LR" node [shape=plaintext]; "c" [label = <
c
c
a
>]; "b_c" [label = <
b_c
b
c
>]; "view" [label = <
view
a
b
c
>]; "b_c":FROM_c -> "c":TO_c; "view":TITLE -> "b_c":TITLE [arrowhead="empty" style="dashed"]; } ') ## ----echo=FALSE--------------------------------------------------------------- if (requireNamespace("DiagrammeR", quietly = TRUE)) DiagrammeR::grViz( 'digraph { rankdir = "LR" node [shape=plaintext]; "a" [label = <
a
a
b
c
>]; "b" [label = <
b
b
d
>]; "c" [label = <
c
c
e
>]; "d_e" [label = <
d_e
d
e
f
>]; "view" [label = <
view
a
b
c
d
e
f
>]; "a":FROM_b -> "b":TO_b; "a":FROM_c -> "c":TO_c; "view":TITLE -> "a":TITLE [arrowhead="empty" style="dashed"]; "view":TITLE -> "d_e":TITLE [arrowhead="empty" style="dashed"]; } ') ## ----echo=FALSE--------------------------------------------------------------- if (requireNamespace("DiagrammeR", quietly = TRUE)) DiagrammeR::grViz( 'digraph { rankdir = "LR" node [shape=plaintext]; "a" [label = <
a
a
b
c
>]; "b" [label = <
b
b
d
>]; "c" [label = <
c
c
e
>]; "d_e" [label = <
d_e
d
e
f
>]; "view" [label = <
view
b
c
d
e
f
>]; "a":FROM_b -> "b":TO_b; "a":FROM_c -> "c":TO_c; "view":TITLE -> "b":TITLE [arrowhead="empty" style="dashed"]; "view":TITLE -> "c":TITLE [arrowhead="empty" style="dashed"]; "view":TITLE -> "d_e":TITLE [arrowhead="empty" style="dashed"]; } ') ## ----echo=FALSE--------------------------------------------------------------- if (requireNamespace("DiagrammeR", quietly = TRUE)) DiagrammeR::grViz( 'digraph { rankdir = "LR" node [shape=plaintext]; "a" [label = <
a
a
b
c
>]; "b" [label = <
b
b
d
>]; "c" [label = <
c
c
e
>]; "d_e" [label = <
d_e
d
e
f
>]; "view" [label = <
view
b
c
d
e
f
>]; "a":FROM_b -> "view":TO_b; "a":FROM_c -> "view":TO_c; "view":TITLE -> "b":TITLE [arrowhead="empty" style="dashed"]; "view":TITLE -> "c":TITLE [arrowhead="empty" style="dashed"]; "view":TITLE -> "d_e":TITLE [arrowhead="empty" style="dashed"]; } ') ## ----echo=FALSE--------------------------------------------------------------- if (requireNamespace("DiagrammeR", quietly = TRUE)) DiagrammeR::grViz( 'digraph { rankdir = "LR" node [shape=plaintext]; "a" [label = <
a
a
b
c
>]; "b" [label = <
b
b
d
>]; "c" [label = <
c
c
e
>]; "d_e" [label = <
d_e
d
e
f
>]; "view" [label = <
view
a
b
c
d
e
f
>]; "view2" [label = <
view
a
b
c
d
e
>]; "view3" [label = <
view
a
b
c
d
>]; "a":FROM_b -> "b":TO_b; "a":FROM_c -> "c":TO_c; "view":TITLE -> "view2":TITLE [arrowhead="empty" style="dashed"]; "view":TITLE -> "d_e":TITLE [arrowhead="empty" style="dashed"]; "view2":TITLE -> "view3":TITLE [arrowhead="empty" style="dashed"]; "view2":TITLE -> "c":TITLE [arrowhead="empty" style="dashed"]; "view3":TITLE -> "a":TITLE [arrowhead="empty" style="dashed"]; "view3":TITLE -> "b":TITLE [arrowhead="empty" style="dashed"]; } ')