-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**What changes are proposed in this pull request?** Adding `print.card()` method. @bzkrouse what do you think of the print method? Once we finalize, I'll finish the PR which will lead to some changes in snapshot testing I think. <img width="787" alt="image" src="https://github.com/insightsengineering/cards/assets/26774684/a2b0f8d7-2142-4c81-9ff1-1a1a987cd104"> What the print method does: - converts object to data frame so the list columns show the values (unlike a tibble print) - prints between 10 and 20 rows - messaging for the number of rows/columns that are not shown - Removes warning and error columns if nothing to report - if there are more than 6 columns, statistic_fmt_fn/contex columns removed - A max of 9 characters are displayed for columns 'group##_level', 'variable_level', 'stat_label', and 'context' - If a statistic is a double, then it is rounded to 3 decimal places - If a proper function is found in the statistic_fmt_fn column, it's printed as "\<fn\>" to save space **Reference GitHub issue associated with pull request.** _e.g., 'closes #1'_ closes #93 -------------------------------------------------------------------------------- Reviewer Checklist (if item does not apply, mark is as complete) - [ ] Ensure all package dependencies are installed: `devtools::install_dev_deps()` - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` - [ ] `usethis::use_spell_check()` runs with no spelling errors in documentation When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] Increment the version number using `usethis::use_version(which = "dev")` - [ ] Run `usethis::use_spell_check()` again - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge".
- Loading branch information
Showing
37 changed files
with
615 additions
and
343 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
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
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,102 @@ | ||
#' | ||
#' Print method for objects of class 'card' | ||
#' | ||
#' @param x object of class 'card' | ||
#' @param n integer specifying the number of rows to print | ||
#' @param columns string indicating whether to print a selected number of | ||
#' columns or all. | ||
#' @param n_col some columns are removed when there are more than a threshold | ||
#' of columns present. This argument sets that threshold. Default is `6L` | ||
#' @param ... not used | ||
#' | ||
#' @return data frame | ||
#' @export | ||
#' | ||
#' @examples | ||
#' ard_categorical(ADSL, variables = AGEGR1) |> | ||
#' print() | ||
print.card <- function(x, n = NULL, columns = c("auto", "all"), n_col = 6L, ...) { | ||
# convert to a data frame so the list columns print the values in the list --- | ||
x_print <- as.data.frame(x) | ||
|
||
# number of rows to print (modeled after tibbles print) ---------------------- | ||
n <- n %||% ifelse(nrow(x_print) > 20L, 10L, nrow(x_print)) | ||
x_print <- utils::head(x_print, n = n) | ||
|
||
# remove columns ------------------------------------------------------------- | ||
if (arg_match(columns) %in% "auto") { | ||
# remove warning and error columns if nothing to report | ||
if ("error" %in% names(x_print) && every(x_print[["error"]], is.null)) | ||
x_print[["error"]] <- NULL | ||
if ("warning" %in% names(x_print) && every(x_print[["warning"]], is.null)) | ||
x_print[["warning"]] <- NULL | ||
if (ncol(x_print) > n_col) | ||
x_print[["statistic_fmt_fn"]] <- NULL # remove this col if there are many cols | ||
if (ncol(x_print) > n_col) | ||
x_print[["context"]] <- NULL # remove this col if there are many cols | ||
} | ||
|
||
# truncate the 'group##_level', 'variable_level', 'stat_label', and 'context' columns ------ | ||
x_print <- | ||
tryCatch( | ||
x_print |> | ||
dplyr::mutate( | ||
across( | ||
c(all_ard_groups(FALSE, TRUE), | ||
all_ard_variables(FALSE, TRUE), | ||
any_of(c("context", "stat_label", "warning", "error"))), | ||
function(x) { | ||
lapply( | ||
x, | ||
function(e) { | ||
e <- as.character(e) |> paste(collapse = ", ") | ||
ifelse(nchar(e) > 9, paste0(substr(e, 1, 8), "\u2026"), e) | ||
} | ||
) | ||
} | ||
) | ||
), | ||
error = function(e) x_print | ||
) | ||
|
||
# for the statistics, round to 3 decimal places ------------------------------ | ||
if ("statistic" %in% names(x_print)) { | ||
x_print$statistic <- lapply( | ||
x_print$statistic, | ||
function(x) { | ||
if (isTRUE(is.double(x))) return(round5(x, digits = 3)) | ||
if (is_string(x) && nchar(x) > 9) return(paste0(substr(x, 1, 8), "\u2026")) | ||
x | ||
} | ||
) | ||
} | ||
|
||
# for the formatting function column, abbreviate the print of proper functions | ||
if ("statistic_fmt_fn" %in% names(x_print)) { | ||
x_print$statistic_fmt_fn <- lapply( | ||
x_print$statistic_fmt_fn, | ||
function(x) { | ||
if (isTRUE(is.function(x))) { | ||
return("<fn>") | ||
} | ||
x | ||
} | ||
) | ||
} | ||
|
||
# final printing -------------------------------------------------------------- | ||
cli::cli_text(cli::col_grey("{{cards}} data frame: {nrow(x)} x {ncol(x)}")) | ||
print(x_print) | ||
if (nrow(x) > n) { | ||
cli::cli_alert_info(cli::col_grey("{nrow(x) - n} more rows")) | ||
cli::cli_alert_info(cli::col_grey("Use {.code print(n = ...)} to see more rows")) | ||
} | ||
if (ncol(x) > ncol(x_print)) { | ||
missing_cols <- names(x) |> setdiff(names(x_print)) | ||
cli::cli_alert_info(cli::col_grey( | ||
"{length(missing_cols)} more variable{?s}: {paste(missing_cols, collapse = ', ')}" | ||
)) | ||
} | ||
invisible(x) | ||
} |
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
Oops, something went wrong.