-
-
Notifications
You must be signed in to change notification settings - Fork 0
add new mode
argument
#182
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,11 +5,11 @@ | |||||||||||
#' @param ... further arguments to be passed to or from other methods. | ||||||||||||
#' @returns a `data.frame` or `list` of `data.frame` filtered for the provided conditions. | ||||||||||||
#' @details | ||||||||||||
#' `log_filter` will filter the data/named list of data according to the `condition`. | ||||||||||||
#' `log_filter` will filter the `data.frame` /named list of `data.frame` according to the `condition`. | ||||||||||||
#' All the variables in `condition` must exist in the data (as variables) or in the parent | ||||||||||||
#' frame(e.g., in global environment). | ||||||||||||
#' For named list of data, if `ADSL` is available, `log_filter` will also try to subset all | ||||||||||||
#' other datasets with `USUBJID`. | ||||||||||||
#' For a named list of `data.frame`, set `mode = "all"`` to filter other tables by keys retained | ||||||||||||
#' in table (using by), or `mode = "unique"` to leave other tables unchanged. | ||||||||||||
#' @export | ||||||||||||
Comment on lines
+12
to
13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for readability |
||||||||||||
log_filter <- function(data, condition, ...) { | ||||||||||||
UseMethod("log_filter") | ||||||||||||
|
@@ -44,29 +44,46 @@ log_filter.data.frame <- function(data, condition, suffix = NULL, ...) { | |||||||||||
|
||||||||||||
#' @rdname log_filter | ||||||||||||
#' @param table (`string`) table name. | ||||||||||||
#' @param by (`character`) variable names shared by `adsl` and other datasets for filtering. | ||||||||||||
#' @param by (`character`) variable names shared by `table` and other datasets for filtering when `mode == "all"`. | ||||||||||||
#' @param verbose (`flag`) whether to print a report about the filtering. | ||||||||||||
#' @param mode (`string`) one of `all` or `unique` whether the other tables should be filtered based on the rows | ||||||||||||
#' retained in `table`. | ||||||||||||
#' Default value is `"all"` is `table == "adsl"` and `"unique"` otherwise. | ||||||||||||
#' @export | ||||||||||||
#' @examples | ||||||||||||
#' log_filter(list(iris = iris), Sepal.Length >= 7, "iris", character(0)) | ||||||||||||
log_filter.list <- function(data, condition, table, by = c("USUBJID", "STUDYID"), suffix = NULL, verbose = FALSE, ...) { | ||||||||||||
log_filter.list <- function(data, | ||||||||||||
condition, | ||||||||||||
table, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be adsl as default maybe? |
||||||||||||
by = c("USUBJID", "STUDYID"), | ||||||||||||
suffix = NULL, | ||||||||||||
verbose = FALSE, | ||||||||||||
mode = ifelse(table == "adsl", "all", "unique"), | ||||||||||||
...) { | ||||||||||||
checkmate::assert_list(data, types = "data.frame", names = "unique") | ||||||||||||
assert_all_tablenames(data, table) | ||||||||||||
checkmate::assert_names(colnames(data[[table]]), must.include = by) | ||||||||||||
checkmate::assert_string(suffix, null.ok = TRUE) | ||||||||||||
checkmate::assert_flag(verbose) | ||||||||||||
checkmate::assert_choice(mode, c("all", "unique")) | ||||||||||||
|
||||||||||||
condition <- match.call()$condition | ||||||||||||
data[[table]] <- eval(bquote(log_filter(data[[table]], .(condition), .(suffix)))) | ||||||||||||
if (identical(table, "adsl")) { | ||||||||||||
for (k in setdiff(names(data), "adsl")) { | ||||||||||||
|
||||||||||||
if (mode == "all") { | ||||||||||||
BFalquet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
checkmate::assert_character(by, null.ok = TRUE) | ||||||||||||
|
||||||||||||
for (k in setdiff(names(data), table)) { | ||||||||||||
if (all(by %in% names(data[[k]]))) { | ||||||||||||
if (length(by) == 0) by <- intersect(names(data[[k]]), names(data$adsl)) | ||||||||||||
if (length(by) == 0) by <- intersect(names(data[[k]]), names(data[[table]])) | ||||||||||||
|
||||||||||||
ori_n <- nrow(data[[k]]) | ||||||||||||
ori_att <- attr(data[[k]], "rows") | ||||||||||||
|
||||||||||||
data[[k]] <- dplyr::semi_join(data[[k]], data$adsl, by = by) | ||||||||||||
data[[k]] <- dplyr::semi_join(data[[k]], data[[table]], by = by) | ||||||||||||
BFalquet marked this conversation as resolved.
Show resolved
Hide resolved
BFalquet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
rows <- list(list(init = ori_n, final = nrow(data[[k]]), suffix = suffix)) | ||||||||||||
names(rows) <- paste0("Filtered by adsl: ", deparse(condition), collapse = "") | ||||||||||||
names(rows) <- paste0(sprintf("Filtered by %s: ", table), deparse(condition), collapse = "") | ||||||||||||
attr(data[[k]], "rows") <- c(ori_att, rows) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,23 @@ test_that("log_filter works with long conditions", { | |
expect_identical(df1, df2, ignore_attr = TRUE) | ||
}) | ||
|
||
test_that("log_filter works with custom `mode` argument", { | ||
dfa <- data.frame(USUBJID = letters[5:14], b = 1:10) | ||
dfb <- data.frame(USUBJID = letters[1:10], c = 1:10) | ||
|
||
attr(dfa$USUBJID, "label") <- "usubjid_dfa" | ||
attr(dfb$USUBJID, "label") <- "usubjid_dfb" | ||
|
||
df_raw <- list(adsl = dfa, dfb = dfb) | ||
res <- expect_silent(log_filter(df_raw, c >= 7, "dfb", by = "USUBJID", mode = "all")) | ||
expect_equal(nrow(res$dfb), 4) | ||
expect_equal(nrow(res$adsl), 4) | ||
|
||
res <- expect_silent(log_filter(df_raw, c >= 7, "dfb", by = "USUBJID", mode = "unique")) | ||
expect_equal(nrow(res$dfb), 4) | ||
expect_equal(nrow(res$adsl), 10) | ||
Comment on lines
+157
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you could show the attribute that was filtered out/in? |
||
}) | ||
|
||
# get_log ---- | ||
|
||
test_that("get_log works as expected", { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.