|
| 1 | +#' Delayed datasets |
| 2 | +#' |
| 3 | +#' Generate `delayed_data_extract_spec` without prior knowledge of the data. |
| 4 | +#' |
| 5 | +#' documentation WIP |
| 6 | +#' |
| 7 | +#' `delayed_datasets` is a character string with class `delayed_datasets` |
| 8 | +#' and attribute `datasets` which is set to `x`. |
| 9 | +#' The attribute specifies a wishlist of datasets for which `delayed_des` are to be created. |
| 10 | +#' |
| 11 | +#' `delayed_data_extract_specs` are resolved as follows: |
| 12 | +#' - `data_extract_specs` are returned as is |
| 13 | +#' - `delayed_data_extract_specs` where `dataname` is `character` are returned as is |
| 14 | +#' - `delayed_data_extract_specs` where `dataname` is `delayed_datasets` is first confronted |
| 15 | +#' with names of datasets in the app and has its `datasets` attribute updated, |
| 16 | +#' and then is converted to a list of `delayed_data_extract_spec`s of the same length as |
| 17 | +#' the updated `datasets` attribute. |
| 18 | +#' |
| 19 | +#' @param x (`character`) set of dataset names for wchich `delayed_data_extract_spec`s will be created; |
| 20 | +#' set to `"all"` to use all available datasets |
| 21 | +#' @param des (`data_extract_spec` or `list` thereof) see `Details` |
| 22 | +#' @param datasets (`character`) vector of dataset for which to resolve |
| 23 | + |
| 24 | +#' @name delayed_datasets |
| 25 | +NULL |
| 26 | + |
| 27 | +#' @rdname delayed_datasets |
| 28 | +#' @export |
| 29 | +delayed_datasets <- function(x = "all") { |
| 30 | + structure( |
| 31 | + "delayed_datasets", |
| 32 | + class = c("delayed_datasets", "delayed_data", "character"), |
| 33 | + datasets = x |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +#' @rdname delayed_datasets |
| 38 | +#' @export |
| 39 | +resolve_delayed_datasets <- function(des, datasets) { |
| 40 | + .resolve_delayed_datasets(.update_delayed_datasets(des, datasets)) |
| 41 | +} |
| 42 | + |
| 43 | +#' @keywords internal |
| 44 | +#' @noRd |
| 45 | +.update_delayed_datasets <- function(des, datasets) { |
| 46 | + .horse <- function(des, datasets) { |
| 47 | + delayed <- attr(des, "datasets", exact = TRUE) |
| 48 | + delayed <- |
| 49 | + if (identical(delayed, "all")) { |
| 50 | + datasets |
| 51 | + } else { |
| 52 | + intersect(delayed, datasets) |
| 53 | + } |
| 54 | + attr(des, "datasets") <- delayed |
| 55 | + des |
| 56 | + } |
| 57 | + |
| 58 | + rapply(des, .horse, "delayed_datasets", how = "replace", datasets = datasets) |
| 59 | +} |
| 60 | + |
| 61 | +#' @keywords internal |
| 62 | +#' @noRd |
| 63 | +.resolve_delayed_datasets <- function(des) { |
| 64 | + .horse <- function(des) { |
| 65 | + if (!inherits(des$dataname, "delayed_datasets")) return(des) |
| 66 | + lapply(attr(des$dataname, "datasets", exact = TRUE), function(dataset) { |
| 67 | + rapply(des, f = function(...) dataset, "delayed_datasets", how = "replace") |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + if (inherits(des, "delayed_data_extract_spec")) return(.horse(des)) |
| 72 | + lapply(des, .resolve_delayed_datasets) |
| 73 | +} |
| 74 | + |
| 75 | +#' ensure that all delayed_datasets in a delayed_des are the same |
| 76 | +assert_delayed_datesets_identical <- function(x) { |
| 77 | + checkmate::assert_class(x, "data_extract_spec") |
| 78 | + if (inherits(x, "delayed_data_extract_spec")) { |
| 79 | + master <- x$dataname |
| 80 | + if (inherits(master, "delayed_datasets")) { |
| 81 | + error_msg <- paste0(deparse1(match.call()), ": delayed_datasets identity violated") |
| 82 | + slaves <- rapply(x, function(xx) xx, "delayed_datasets", how = "unlist") |
| 83 | + slaves_datasets <- rapply(x, function(xx) attr(xx, "datasets"), "delayed_datasets", how = "unlist") |
| 84 | + Reduce( |
| 85 | + function(x1, x2) { |
| 86 | + if (identical(x1, x2)) x2 else stop(error_msg, call. = FALSE) |
| 87 | + }, |
| 88 | + slaves, |
| 89 | + init = as.vector(master) |
| 90 | + ) |
| 91 | + Reduce( |
| 92 | + function(x1, x2) { |
| 93 | + if (identical(x1, x2)) x2 else stop(error_msg, call. = FALSE) |
| 94 | + }, |
| 95 | + slaves_datasets, |
| 96 | + init = attr(master, "datasets") |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + x |
| 101 | +} |
0 commit comments