-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 767_sort_filter_hierarchical
- Loading branch information
Showing
16 changed files
with
222 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#' Variable Group Header | ||
#' | ||
#' @description | ||
#' Some data are inherently grouped, and should be reported together. | ||
#' Grouped variables are all indented together. | ||
#' This function indents the variables that should be reported together while | ||
#' adding a header above the group. | ||
#' | ||
#' @inheritParams modify_column_indent | ||
#' @param x (`tbl_summary`)\cr | ||
#' gtsummary object of class `'tbl_summary'` | ||
#' @param header (`string`)\cr | ||
#' string of the header to place above the variable group | ||
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr | ||
#' Variables to group that appear in `x$table_body`. | ||
#' Selected variables should be appear consecutively in table. | ||
#' | ||
#' @returns a gtsummary table | ||
#' @export | ||
#' | ||
#' @details | ||
#' This function works by inserting a row into the `x$table_body` and | ||
#' indenting the group of selected variables. | ||
#' This function cannot be used in conjunction with all functions in gtsummary; | ||
#' for example, `bold_labels()` will bold the incorrect rows after running | ||
#' this function. | ||
#' | ||
#' @examples | ||
#' # Example 1 ---------------------------------- | ||
#' set.seed(11234) | ||
#' data.frame( | ||
#' exclusion_age = sample(c(TRUE, FALSE), 20, replace = TRUE), | ||
#' exclusion_mets = sample(c(TRUE, FALSE), 20, replace = TRUE), | ||
#' exclusion_physician = sample(c(TRUE, FALSE), 20, replace = TRUE) | ||
#' ) |> | ||
#' tbl_summary( | ||
#' label = list(exclusion_age = "Age", | ||
#' exclusion_mets = "Metastatic Disease", | ||
#' exclusion_physician = "Physician") | ||
#' ) |> | ||
#' add_variable_group_header( | ||
#' header = "Exclusion Reason", | ||
#' variables = starts_with("exclusion_") | ||
#' ) |> | ||
#' modify_caption("**Study Exclusion Criteria**") | ||
add_variable_group_header <- function(x, header, variables, indent = 4L) { | ||
# check inputs --------------------------------------------------------------- | ||
set_cli_abort_call() | ||
check_class(x, "tbl_summary") | ||
check_string(header) | ||
check_scalar_integerish(indent) | ||
|
||
# process variables ---------------------------------------------------------- | ||
cards::process_selectors(scope_table_body(x$table_body), variables = {{ variables }}) | ||
# return unaltered table if no variables selected | ||
if (is_empty(variables)) return(x) # styler: off | ||
|
||
# add header above the first variable selected ------------------------------- | ||
df_insert <- dplyr::tibble(row_type = "variable_group", label = header) | ||
# identify row where to insert header | ||
idx <- which(x$table_body$variable %in% variables)[1] | ||
|
||
x |> | ||
modify_table_body(~ dplyr::add_row(.x, df_insert, .before = idx)) |> | ||
.modify_column_indent( | ||
columns = "label", | ||
rows = .data$variable %in% .env$variables, | ||
indent = indent | ||
) | ||
} |
This file contains 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 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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
test_that("add_variable_group_header() works", { | ||
expect_silent( | ||
tbl <- trial |> | ||
tbl_summary(include = c(age, response, death, marker), missing = "no") |> | ||
add_variable_group_header(header = "the header row", variables = c(response, death)) | ||
) | ||
|
||
# header row is in correct location | ||
expect_equal( | ||
as_tibble(tbl, col_labels = FALSE)$label[2], | ||
"the header row" | ||
) | ||
|
||
# grouped variables are indented. | ||
expect_equal( | ||
.table_styling_expr_to_row_number(tbl)$table_styling$indent$row_numbers[[1]], | ||
c(3L, 4L) | ||
) | ||
}) |
This file contains 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