Manage relationships between datasets using join_keys
Source: R/join_keys.R
, R/join_keys-extract.R
, R/join_keys-c.R
, and 1 more
join_keys.Rd
Facilitates the creation and retrieval of relationships between datasets.
join_keys
class extends list
and contains keys connecting pairs of datasets.
Each element of the list contains keys for specific dataset.
Each dataset can have a relationship with itself (primary key) and with other datasets.
Note that join_keys
list is symmetrical and assumes a default direction, that is:
when keys are set between ds1
and ds2
, it defines ds1
as the parent
in a parent-child relationship and the mapping is automatically mirrored between
ds2
and ds1
.
Usage
## Constructor, getter and setter
join_keys(...)
# S3 method for default
join_keys(...)
# S3 method for join_keys
join_keys(...)
# S3 method for teal_data
join_keys(...)
# S3 method for join_keys
[(x, i, j)
# S3 method for join_keys
[(x, i, j, directed = TRUE) <- value
# S3 method for join_keys
c(...)
# S3 method for join_key_set
c(...)
join_keys(x) <- value
# S3 method for join_keys
join_keys(x) <- value
# S3 method for teal_data
join_keys(x) <- value
# S3 method for join_keys
format(x, ...)
# S3 method for join_keys
print(x, ...)
Arguments
- ...
-
optional,
either
teal_data
orjoin_keys
object to extractjoin_keys
or any number of
join_key_set
objects to createjoin_keys
or nothing to create an empty
join_keys
- x
(
join_keys
) empty object to set the new relationship pairs.x
is typically an object ofjoin_keys
class. When called with thejoin_keys(x)
orjoin_keys(x) <- value
then it can also take a supported class (teal_data
,join_keys
)- i, j
indices specifying elements to extract or replace. Index should be a a character vector, but it can also take numeric, logical,
NULL
or missing.- directed
-
(
logical(1)
) Flag that indicates whether it should create a parent-child relationship between the datasets.TRUE
(default)dataset_1
is the parent ofdataset_2
;FALSE
when the relationship is undirected.
- value
-
For
x[i, j, directed = TRUE)] <- value
(named/unnamedcharacter
) Column mapping between datasets.For
join_keys(x) <- value
: (join_key_set
or list ofjoin_key_set
) relationship pairs to add tojoin_keys
list.[i, j, directed = TRUE)]: R:i,%20j,%20directed%20=%20TRUE)
Methods (by class)
join_keys()
: Returns an emptyjoin_keys
object when called without arguments.join_keys(join_keys)
: Returns itself.join_keys(teal_data)
: Returns thejoin_keys
object contained inteal_data
object.join_keys(...)
: Creates a new object with one or morejoin_key_set
parameters.
Functions
x[datanames]
: Returns a subset of thejoin_keys
object for givendatanames
, including parentdatanames
and symmetric mirror keys betweendatanames
in the result.x[i, j]
: Returns join keys between datasetsi
andj
, including implicit keys inferred from their relationship with a parent.
x[i, j] <- value
: Assignment of a key to pair(i, j)
.x[i] <- value
: This (withoutj
parameter) is not a supported operation forjoin_keys
.join_keys(x)[i, j] <- value
: Assignment tojoin_keys
object stored inx
, such as ateal_data
object orjoin_keys
object itself.
join_keys(x) <- value
: Assignment of thejoin_keys
in object withvalue
.value
needs to be an object of classjoin_keys
orjoin_key_set
.
See also
join_key()
for creating join_keys_set
,
parents()
for parent operations,
teal_data()
for teal_data
constructor and
default_cdisc_join_keys for default CDISC keys.
Examples
# Creating a new join keys ----
jk <- join_keys(
join_key("ds1", "ds1", "pk1"),
join_key("ds2", "ds2", "pk2"),
join_key("ds3", "ds3", "pk3"),
join_key("ds1", "ds2", c(pk1 = "pk2")),
join_key("ds1", "ds3", c(pk1 = "pk3"))
)
jk
#> A join_keys object containing foreign keys between 3 datasets:
#> ds1: [pk1]
#> <-- ds2: [pk2]
#> <-- ds3: [pk3]
#> ds2: [pk2]
#> --> ds1: [pk1]
#> --* (implicit via parent with): ds3
#> ds3: [pk3]
#> --> ds1: [pk1]
#> --* (implicit via parent with): ds2
# Getter for join_keys ---
jk["ds1", "ds2"]
#> pk1
#> "pk2"
# Subsetting join_keys ----
jk["ds1"]
#> A join_keys object containing foreign keys between 1 datasets:
#> ds1: [pk1]
jk[1:2]
#> A join_keys object containing foreign keys between 2 datasets:
#> ds1: [pk1]
#> <-- ds2: [pk2]
#> ds2: [pk2]
#> --> ds1: [pk1]
jk[c("ds1", "ds2")]
#> A join_keys object containing foreign keys between 2 datasets:
#> ds1: [pk1]
#> <-- ds2: [pk2]
#> ds2: [pk2]
#> --> ds1: [pk1]
# Setting a new primary key ---
jk["ds4", "ds4"] <- "pk4"
jk["ds5", "ds5"] <- "pk5"
# Setting a single relationship pair ---
jk["ds1", "ds4"] <- c("pk1" = "pk4")
# Removing a key ---
jk["ds5", "ds5"] <- NULL
# Merging multiple `join_keys` objects ---
jk_merged <- c(
jk,
join_keys(
join_key("ds4", keys = c("pk4", "pk4_2")),
join_key("ds3", "ds4", c(pk3 = "pk4_2"))
)
)
# note: merge can be performed with both join_keys and join_key_set
jk_merged <- c(
jk_merged,
join_key("ds5", keys = "pk5"),
join_key("ds1", "ds5", c(pk1 = "pk5"))
)
# Assigning keys via join_keys(x)[i, j] <- value ----
obj <- join_keys()
# or
obj <- teal_data()
join_keys(obj)["ds1", "ds1"] <- "pk1"
join_keys(obj)["ds2", "ds2"] <- "pk2"
join_keys(obj)["ds3", "ds3"] <- "pk3"
join_keys(obj)["ds1", "ds2"] <- c(pk1 = "pk2")
join_keys(obj)["ds1", "ds3"] <- c(pk1 = "pk3")
identical(jk, join_keys(obj))
#> [1] FALSE
# Setter for join_keys within teal_data ----
td <- teal_data()
join_keys(td) <- jk
join_keys(td)["ds1", "ds2"] <- "new_key"
join_keys(td) <- c(join_keys(td), join_keys(join_key("ds3", "ds2", "key3")))
join_keys(td)
#> A join_keys object containing foreign keys between 4 datasets:
#> ds1: [pk1]
#> <-> ds2: [new_key]
#> <-- ds3: [pk3]
#> <-- ds4: [pk4]
#> ds3: [pk3]
#> --> ds1: [pk1]
#> <-- ds2: [key3]
#> --* (implicit via parent with): ds4
#> ds2: [pk2]
#> <-> ds1: [new_key]
#> --> ds3: [key3]
#> ds4: [pk4]
#> --> ds1: [pk1]
#> --* (implicit via parent with): ds3