-
Notifications
You must be signed in to change notification settings - Fork 8
Rework key_colnames
#592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brookslogan
merged 3 commits into
lcb/key_colnames-revision_summary-age_agg-updates
from
lcb/rework-key_colnames
Jan 24, 2025
Merged
Rework key_colnames
#592
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,123 @@ | ||
| #' Grab any keys associated to an epi_df | ||
| #' Get names of columns that form a (unique) key associated with an object | ||
| #' | ||
| #' @param x a data.frame, tibble, or epi_df | ||
| #' This is entirely based on metadata and arguments passed; there are no | ||
| #' explicit checks that the key actually is unique in any associated data | ||
| #' structures. | ||
| #' | ||
| #' @param x an object, such as an [`epi_df`] | ||
| #' @param ... additional arguments passed on to methods | ||
| #' @param other_keys an optional character vector of other keys to include | ||
| #' @param exclude an optional character vector of keys to exclude | ||
| #' @return If an `epi_df`, this returns all "keys". Otherwise `NULL`. | ||
| #' @param geo_keys optional character vector; which columns (if any) to consider | ||
| #' keys specifying the geographical region? Defaults to `"geo_value"` if | ||
| #' present; must be `"geo_value"` if `x` is an `epi_df`. | ||
| #' @param other_keys character vector; which columns (if any) to consider keys | ||
| #' specifying demographical or identifying/grouping information besides the | ||
| #' geographical region and time interval? Mandatory if `x` is a vanilla | ||
| #' `data.frame` or `tibble`. Optional if `x` is an `epi_df`; default is the | ||
| #' `epi_df`'s `other_keys`; if you provide `other_keys`, they must match the | ||
| #' default. (This behavior is to enable consistent and sane results when you | ||
| #' can't guarantee whether `x` is an `epi_df` or just a | ||
| #' `tibble`/`data.frame`.) | ||
| #' @param time_keys optional character vector; which columns (if any) to | ||
| #' consider keys specifying the time interval during which associated events | ||
| #' occurred? Defaults to `"time_value"` if present; must be `"time_value"` if | ||
| #' `x` is an `epi_df`. | ||
| #' @param exclude an optional character vector of key column names to exclude | ||
| #' from the result | ||
| #' @return character vector | ||
| #' @keywords internal | ||
| #' @export | ||
| key_colnames <- function(x, ...) { | ||
| key_colnames <- function(x, ..., exclude = character()) { | ||
| UseMethod("key_colnames") | ||
| } | ||
|
|
||
| #' @rdname key_colnames | ||
| #' @method key_colnames default | ||
| #' @export | ||
| key_colnames.default <- function(x, ...) { | ||
| character(0L) | ||
| } | ||
|
|
||
| #' @rdname key_colnames | ||
| #' @importFrom rlang check_dots_empty0 | ||
| #' @method key_colnames data.frame | ||
| #' @export | ||
| key_colnames.data.frame <- function(x, other_keys = character(0L), exclude = character(0L), ...) { | ||
| key_colnames.data.frame <- function(x, ..., | ||
| geo_keys = intersect("geo_value", names(x)), | ||
| other_keys, | ||
| time_keys = intersect("time_value", names(x)), | ||
| exclude = character()) { | ||
| check_dots_empty0(...) | ||
| assert_character(geo_keys) | ||
| assert_character(time_keys) | ||
| assert_character(other_keys) | ||
| assert_character(exclude) | ||
| nm <- setdiff(c("geo_value", other_keys, "time_value"), exclude) | ||
| intersect(nm, colnames(x)) | ||
| keys = c(geo_keys, other_keys, time_keys) | ||
| if (!all(keys %in% names(x))) { | ||
| cli_abort(c( | ||
| "Some of the specified key columns aren't present in `x`", | ||
| "i" = "Specified keys: {format_varnames(keys)}", | ||
| "i" = "Columns of x: {format_varnames(names(x))}", | ||
| "x" = "Missing keys: {format_varnames(setdiff(keys, names(x)))}" | ||
| ), class = "epiprocess__key_colnames__keys_not_in_colnames") | ||
| } | ||
| setdiff(keys, exclude) | ||
| } | ||
|
|
||
| #' @rdname key_colnames | ||
| #' @method key_colnames epi_df | ||
| #' @export | ||
| key_colnames.epi_df <- function(x, exclude = character(0L), ...) { | ||
| key_colnames.epi_df <- function(x, ..., | ||
| geo_keys = "geo_value", | ||
| other_keys = NULL, | ||
brookslogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| time_keys = "time_value", | ||
| exclude = character()) { | ||
| check_dots_empty0(...) | ||
| if (!identical(geo_keys, "geo_value")) { | ||
| cli_abort('If `x` is an `epi_df`, then `geo_keys` must be `"geo_value"`', | ||
| class = "epiprocess__key_colnames__mismatched_geo_keys") | ||
| } | ||
| if (!identical(time_keys, "time_value")) { | ||
| cli_abort('If `x` is an `epi_df`, then `time_keys` must be `"time_value"`', | ||
| class = "epiprocess__key_colnames__mismatched_time_keys") | ||
| } | ||
| expected_other_keys <- attr(x, "metadata")$other_keys | ||
| if (is.null(other_keys)) { | ||
| other_keys <- expected_other_keys | ||
| } else { | ||
| if (!identical(other_keys, expected_other_keys)) { | ||
| cli_abort(c( | ||
| "The provided `other_keys` argument didn't match the `other_keys` of `x`", | ||
| "*" = "`other_keys` was {format_chr_with_quotes(other_keys)}", | ||
| "*" = "`expected_other_keys` was {format_chr_with_quotes(expected_other_keys)}", | ||
| "i" = "If you know that `x` will always be an `epi_df` and | ||
| resolve this discrepancy by adjusting the metadata of `x`, you | ||
| shouldn't have to pass `other_keys =` here anymore, | ||
| unless you want to continue to perform this check." | ||
| ), class = "epiprocess__key_colnames__mismatched_other_keys") | ||
| } | ||
| } | ||
| assert_character(exclude) | ||
| other_keys <- attr(x, "metadata")$other_keys | ||
| setdiff(c("geo_value", other_keys, "time_value"), exclude) | ||
| } | ||
|
|
||
| #' @rdname key_colnames | ||
| #' @method key_colnames tbl_ts | ||
| #' @export | ||
| key_colnames.tbl_ts <- function(x, ..., exclude = character()) { | ||
| check_dots_empty0(...) | ||
| assert_character(exclude) | ||
| idx <- tsibble::index_var(x) | ||
| idx2 <- tsibble::index2_var(x) | ||
| if (!identical(idx, idx2)) { | ||
| cli_abort(c( | ||
| "`x` is in the middle of a re-indexing operation with `index_by()`; it's unclear | ||
| whether we should output the old unique key or the new unique key-to-be", | ||
| "i" = "Old index: {format_varname(idx)}", | ||
| "i" = "Pending new index: {format_varname(idx2)}", | ||
| "Please complete (e.g., with `summarise()`) or remove the re-indexing operation." | ||
| ), class = "epiprocess__key_colnames__incomplete_reindexing_operation") | ||
| } | ||
| setdiff(c(tsibble::key_vars(x), idx), exclude) | ||
| } | ||
|
|
||
| #' @rdname key_colnames | ||
| #' @method key_colnames epi_archive | ||
| #' @export | ||
| key_colnames.epi_archive <- function(x, exclude = character(0L), ...) { | ||
| key_colnames.epi_archive <- function(x, ..., exclude = character()) { | ||
| check_dots_empty0(...) | ||
| assert_character(exclude) | ||
| other_keys <- attr(x, "metadata")$other_keys | ||
| setdiff(c("geo_value", other_keys, "time_value"), exclude) | ||
| setdiff(c("geo_value", x$other_keys, "time_value", "version"), exclude) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.