Skip to content

Commit

Permalink
various
Browse files Browse the repository at this point in the history
  • Loading branch information
gogonzo committed Nov 14, 2023
1 parent 923b251 commit 5d4d1a6
Show file tree
Hide file tree
Showing 22 changed files with 77 additions and 429 deletions.
3 changes: 1 addition & 2 deletions R/get_join_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ get_join_keys <- function(data) {
}

#' @rdname get_join_keys
#' @inheritParams join_keys
#' @param dataset_2 (`character(1)`) name of a dataset.
#' @inheritParams join_key
#' @param value value to assign
#' @export
`get_join_keys<-` <- function(data, dataset_1, dataset_2 = NULL, value) {
Expand Down
4 changes: 2 additions & 2 deletions R/join_key.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#' @details `join_key()` will create a relationship for the variables on a pair
#' of datasets.
#'
#' @inheritParams join_keys
#' @param dataset_2 (optional `character`) other dataset name. In case it is omitted, then it
#' @param dataset_1 (`character(1)`) dataset name.
#' @param dataset_2 (optional `character(1)`) other dataset name. In case it is omitted, then it
#' will create a primary key for `dataset_1`.
#' @param keys (optionally named `character`) where `names(keys)` are columns in `dataset_1`
#' with relationship to columns of `dataset_2` given by the elements in `keys`.
Expand Down
49 changes: 14 additions & 35 deletions R/join_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ join_keys.default <- function(...) {
#' The setter assignment `join_keys(obj) <- ...` will merge obj and `...` if obj
#' is not empty.
#'
#' @param join_keys_obj (`join_keys`) empty object to set the new relationship pairs.
#' @param x (`join_keys`) empty object to set the new relationship pairs.
#' @param value (`join_key_set` or list of `join_key_set`) relationship pairs to add
#' to `join_keys` list.
#'
#' @export
`join_keys<-` <- function(join_keys_obj, value) {
`join_keys<-` <- function(x, value) {
checkmate::assert(
combine = "or",
checkmate::check_class(value, classes = c("join_keys", "list")),
checkmate::check_class(value, classes = c("join_key_set")),
checkmate::check_list(value, types = "join_key_set")
)
UseMethod("join_keys<-", join_keys_obj)
UseMethod("join_keys<-", x)
}

#' @rdname join_keys
Expand All @@ -123,7 +123,7 @@ join_keys.default <- function(...) {
#'
#' join_keys(jk)[["ds1"]][["ds3"]] <- "some_col3"
#' jk
`join_keys<-.join_keys` <- function(join_keys_obj, value) {
`join_keys<-.join_keys` <- function(x, value) {
# Assume assignment of join keys as a merge operation
# Needed to support join_keys(jk)[c("ds1", "ds2")] <- "key"
if (checkmate::test_class(value, classes = c("join_keys", "list"))) {
Expand Down Expand Up @@ -160,9 +160,9 @@ join_keys.default <- function(...) {
#' join_keys(td)[["ds2"]][["ds2"]] <- "key2"
#' join_keys(td) <- c(join_keys(td), join_keys(join_key("ds3", "ds2", "key3")))
#' join_keys(td)
`join_keys<-.teal_data` <- function(join_keys_obj, value) {
join_keys(join_keys_obj@join_keys) <- value
join_keys_obj
`join_keys<-.teal_data` <- function(x, value) {
join_keys(x@join_keys) <- value
x
}

#' @rdname join_keys
Expand Down Expand Up @@ -224,7 +224,7 @@ c.join_keys <- function(...) {
#' @details
#' Getter for `join_keys` that returns the relationship between pairs of datasets.
#'
#' @inheritParams base::`[`
#' @inheritParams base::`[[`
#' @param keep_all_foreign_keys (`logical`) flag that keeps foreign keys and other
#' datasets even if they are not a parent of the selected dataset.
#'
Expand Down Expand Up @@ -315,27 +315,6 @@ c.join_keys <- function(...) {
#' @inheritParams base::`[<-`
#'
#' @export
#'
#' @examples
#'
#' # Setter via index ----
#'
#' jk <- join_keys(
#' join_key("ds1", "ds2", c("id_1" = "id_2")),
#' join_key("ds3", "ds4", c("id_3" = "id_4"))
#' )
#'
#' # overwrites previously defined key
#' jk["ds1"] <- list(ds2 = "(new)co12")
#' jk["ds1"] <- list(ds3 = "col13", ds4 = "col14")
#' jk
#'
#' jk[c("ds1", "ds2")] <- list(ds5 = "col*5")
#' jk[c(1, 2)] <- list(ds5 = "col**5")
#'
#' # Creates primary key by only defining `i`
#' jk["ds1"] <- "primary_key"
#' jk
`[<-.join_keys` <- function(x, i, value) {
stop("Can't use `[<-` for object `join_keys`. Use [[<- instead.")
}
Expand Down Expand Up @@ -432,7 +411,7 @@ c.join_keys <- function(...) {
)
preserve_attr <- attributes(x)[!names(attributes(x)) %in% "names"]
x <- x[!empty_ix]
attributes(x) <- modifyList(attributes(x), preserve_attr)
attributes(x) <- utils::modifyList(attributes(x), preserve_attr)

#
# restore class
Expand Down Expand Up @@ -545,13 +524,13 @@ assert_compatible_keys <- function(join_key_1, join_key_2) {

#' Helper function checks the parent-child relations are valid
#'
#' @param join_keys_obj (`join_keys`) object to assert validity of relations
#' @param x (`join_keys`) object to assert validity of relations
#'
#' @return `join_keys_obj` invisibly
#' @return `join_keys` invisibly
#'
#' @keywords internal
assert_parent_child <- function(join_keys_obj) {
jk <- join_keys(join_keys_obj)
assert_parent_child <- function(x) {
jk <- join_keys(x)
jk_parents <- parents(jk)

checkmate::assert_class(jk, c("join_keys", "list"))
Expand All @@ -575,5 +554,5 @@ assert_parent_child <- function(join_keys_obj) {
}
}
}
invisible(join_keys_obj)
invisible(x)
}
50 changes: 25 additions & 25 deletions R/parents.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Getter and setter for specific parent
#'
#' @param join_keys_obj (`join_keys`) object to retrieve.
#' @param x (`join_keys`) object to retrieve.
#' @param dataset_name (`character(1)`)
#'
#' @export
Expand All @@ -10,29 +10,29 @@
#' parent(jk, "ds2")
#' parents(jk) <- list("ds2" = "ds1")
#' parent(jk, "ds2")
parent <- function(join_keys_obj, dataset_name) {
parent <- function(x, dataset_name) {
checkmate::assert_string(dataset_name)
# assert join_keys_obj is performed by parents()
parents(join_keys_obj)[[dataset_name]]
# assert x is performed by parents()
parents(x)[[dataset_name]]
}

#' Getter and setter functions for parents attribute of `join_keys`
#'
#' @param join_keys_obj (`join_keys`) object to retrieve or manipulate.
#' @param x (`join_keys`) object to retrieve or manipulate.
#' @return a list of `character` representing the parents.
#'
#' @export
parents <- function(join_keys_obj) {
UseMethod("parents", join_keys_obj)
parents <- function(x) {
UseMethod("parents", x)
}

#' @rdname parents
#' @export
#' @examples
#' jk <- default_cdisc_join_keys["ADEX"]
#' parents(jk)
parents.join_keys <- function(join_keys_obj) {
attr(join_keys_obj, "__parents__") %||% list()
parents.join_keys <- function(x) {
attr(x, "__parents__") %||% list()
}

#' @rdname parents
Expand All @@ -44,17 +44,17 @@ parents.join_keys <- function(join_keys_obj) {
#' ADTTE = teal.data::rADTTE
#' )
#' parents(td)
parents.teal_data <- function(join_keys_obj) {
attr(join_keys_obj@join_keys, "__parents__") %||% list()
parents.teal_data <- function(x) {
attr(x@join_keys, "__parents__") %||% list()
}

#' @rdname parents
#'
#' @param value (`list`) named list of character values
#'
#' @export
`parents<-` <- function(join_keys_obj, value) {
UseMethod("parents<-", join_keys_obj)
`parents<-` <- function(x, value) {
UseMethod("parents<-", x)
}

#' @rdname parents
Expand All @@ -70,7 +70,7 @@ parents.teal_data <- function(join_keys_obj) {
#' parents(jk) <- list(ds1 = "ds2")
#' parents(jk)["ds5"] <- "ds6"
#' parents(jk)["ds6"] <- "ds7"
`parents<-.join_keys` <- function(join_keys_obj, value) {
`parents<-.join_keys` <- function(x, value) {
checkmate::assert_list(value, types = "character", names = "named")

new_parents <- list()
Expand All @@ -95,10 +95,10 @@ parents.teal_data <- function(join_keys_obj) {
stop("Cycle detected in a parent and child dataset graph.")
}

attr(join_keys_obj, "__parents__") <- new_parents # nolint: object_name_linter
attr(x, "__parents__") <- new_parents # nolint: object_name_linter

assert_parent_child(join_keys_obj)
join_keys_obj
assert_parent_child(x)
x
}

#' @rdname parents
Expand All @@ -112,22 +112,22 @@ parents.teal_data <- function(join_keys_obj) {
#' )
#' parents(td) <- list("ADTTE" = "ADSL") # replace existing
#' parents(td)["ADRS"] <- "ADSL" # add new parent
`parents<-.teal_data` <- function(join_keys_obj, value) {
parents(join_keys_obj@join_keys) <- value
join_keys_obj
`parents<-.teal_data` <- function(x, value) {
parents(x@join_keys) <- value
x
}

#' Updates the keys of the datasets based on the parents.
#'
#' @param join_keys_obj (`join_keys`) object to update the keys.
#' @param x (`join_keys`) object to update the keys.
#'
#' @return (`self`) invisibly for chaining
#'
#' @keywords internal
update_keys_given_parents <- function(join_keys_obj) {
jk <- join_keys(join_keys_obj)
update_keys_given_parents <- function(x) {
jk <- join_keys(x)

checkmate::assert_class(jk, "join_keys", .var.name = checkmate::vname(join_keys_obj))
checkmate::assert_class(jk, "join_keys", .var.name = checkmate::vname(x))

datanames <- names(jk)
duplicate_pairs <- list()
Expand Down Expand Up @@ -161,7 +161,7 @@ update_keys_given_parents <- function(join_keys_obj) {
}
}
# check parent child relation
assert_parent_child(join_keys_obj = jk)
assert_parent_child(x = jk)

jk
}
12 changes: 6 additions & 6 deletions R/teal_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ teal_data_file <- function(path, code = get_code(path)) {
#' Add primary keys as join_keys to a dataset self
#'
#' @param data_objects (`list`) of `TealDataset`, `TealDatasetConnector` or `TealDataConnector` objects
#' @param join_keys_obj (`join_keys`) object
#' @param x (`join_keys`) object
#'
#' @keywords internal
update_join_keys_to_primary <- function(data_objects, join_keys_obj) {
update_join_keys_to_primary <- function(data_objects, x) {
for (obj in data_objects) {
if (inherits(obj, "TealDataConnector")) {
join_keys_obj <- update_join_keys_to_primary(obj$get_items(), join_keys_obj)
x <- update_join_keys_to_primary(obj$get_items(), x)
} else {
dataname <- obj$get_dataname()
if (length(join_keys_obj[[dataname]][[dataname]]) == 0) {
join_keys_obj[[dataname]][[dataname]] <- obj$get_keys()
if (length(x[[dataname]][[dataname]]) == 0) {
x[[dataname]][[dataname]] <- obj$get_keys()
}
}
}
join_keys_obj
x
}
Loading

0 comments on commit 5d4d1a6

Please sign in to comment.