diff --git a/R/as_kable_extra.R b/R/as_kable_extra.R index 448267737..ea43c539b 100644 --- a/R/as_kable_extra.R +++ b/R/as_kable_extra.R @@ -347,6 +347,37 @@ table_styling_to_kable_extra_calls <- function(x, escape, format, addtl_fmt, ... ) } + # source note ---------------------------------------------------------------- + kable_extra_calls[["source_note"]] <- + map( + seq_len(nrow(x$table_styling$source_note)), + \(i) { + expr( + kableExtra::footnote(number = !!x$table_styling$source_note$source_note[i], escape = !!escape) + ) + } + ) + + # abbreviation --------------------------------------------------------------- + kable_extra_calls[["abbreviations"]] <- + case_switch( + nrow(x$table_styling$abbreviation) > 0L ~ + expr( + kableExtra::footnote( + general = !!(x$table_styling$abbreviation$abbreviation |> + paste(collapse = ", ") %>% + paste0( + ifelse(nrow(x$table_styling$abbreviation) > 1L, "Abbreviations", "Abbreviation") |> translate_string(), + ": ", . + )), + escape = !!escape, + general_title = "", + footnote_order = c("number", "alphabet", "symbol", "general") + ) + ), + .default = list() + ) + # footnote ------------------------------------------------------------------- vct_footnote <- dplyr::bind_rows( @@ -361,35 +392,6 @@ table_styling_to_kable_extra_calls <- function(x, escape, format, addtl_fmt, ... expr(kableExtra::footnote(number = !!vct_footnote, escape = !!escape)) } - # abbreviation --------------------------------------------------------------- - kable_extra_calls[["abbreviations"]] <- - case_switch( - nrow(x$table_styling$abbreviation) > 0L ~ - expr( - kableExtra::footnote( - number = !!(x$table_styling$abbreviation$abbreviation |> - paste(collapse = ", ") %>% - paste0( - ifelse(nrow(x$table_styling$abbreviation) > 1L, "Abbreviations", "Abbreviation") |> translate_string(), - ": ", . - )), - escape = !!escape - ) - ), - .default = list() - ) - - # source note ---------------------------------------------------------------- - kable_extra_calls[["source_note"]] <- - map( - seq_len(nrow(x$table_styling$source_note)), - \(i) { - expr( - kableExtra::footnote(number = !!x$table_styling$source_note$source_note[i], escape = !!escape) - ) - } - ) - # return list of calls ------------------------------------------------------- kable_extra_calls } diff --git a/R/modify_abbreviation.R b/R/modify_abbreviation.R index edde312b6..f736adc9d 100644 --- a/R/modify_abbreviation.R +++ b/R/modify_abbreviation.R @@ -10,7 +10,8 @@ #' @return Updated gtsummary object #' @name modify_abbreviation #' -#' @examples +#' @examplesIf (identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true")) && gtsummary:::is_pkg_installed(c("cardx", "broom", "broom.helpers")) +#' # Example 1 ---------------------------------- #' tbl_summary( #' trial, #' by = trt, @@ -19,6 +20,11 @@ #' ) |> #' modify_table_body(~dplyr::mutate(.x, label = sub("Q1, Q3", "IQR", x = label))) |> #' modify_abbreviation("IQR = Interquartile Range") +#' +#' # Example 2 ---------------------------------- +#' lm(marker ~ trt, trial) |> +#' tbl_regression() |> +#' remove_abbreviation("CI = Confidence Interval") NULL #' @export @@ -55,7 +61,7 @@ remove_abbreviation <- function(x, abbreviation) { } if (!isTRUE(abbreviation %in% x$table_styling$abbreviation$abbreviation)) { cli::cli_abort( - "The {.arg abbreviation} must be one of {.val {unique(x$table_styling$abbreviation$abbreviation)}}.", + "The {.arg abbreviation} argument must be one of {.val {unique(x$table_styling$abbreviation$abbreviation)}}.", call = get_cli_abort_call() ) } diff --git a/R/tbl_continuous.R b/R/tbl_continuous.R index daa5189ac..431f57842 100644 --- a/R/tbl_continuous.R +++ b/R/tbl_continuous.R @@ -20,7 +20,7 @@ #' @return a gtsummary table #' @export #' -#' @examples +#' @examplesIf identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true") #' # Example 1 ---------------------------------- #' tbl_continuous( #' data = trial, diff --git a/R/tbl_regression.R b/R/tbl_regression.R index b594d004d..ff327076e 100644 --- a/R/tbl_regression.R +++ b/R/tbl_regression.R @@ -69,7 +69,7 @@ #' @name tbl_regression #' @return A `tbl_regression` object #' -#' @examplesIf gtsummary:::is_pkg_installed(c("cardx", "broom", "broom.helpers")) +#' @examplesIf (identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true")) && gtsummary:::is_pkg_installed(c("cardx", "broom", "broom.helpers")) #' # Example 1 ---------------------------------- #' glm(response ~ age + grade, trial, family = binomial()) |> #' tbl_regression(exponentiate = TRUE) diff --git a/man/modify_abbreviation.Rd b/man/modify_abbreviation.Rd index 83e9f2585..8a857d867 100644 --- a/man/modify_abbreviation.Rd +++ b/man/modify_abbreviation.Rd @@ -29,6 +29,7 @@ All abbreviations will be coalesced when printing the final table into a single source note. } \examples{ +# Example 1 ---------------------------------- tbl_summary( trial, by = trt, @@ -37,4 +38,9 @@ tbl_summary( ) |> modify_table_body(~dplyr::mutate(.x, label = sub("Q1, Q3", "IQR", x = label))) |> modify_abbreviation("IQR = Interquartile Range") + +# Example 2 ---------------------------------- +lm(marker ~ trt, trial) |> + tbl_regression() |> + remove_abbreviation("CI = Confidence Interval") } diff --git a/tests/testthat/_snaps/as_kable_extra.md b/tests/testthat/_snaps/as_kable_extra.md index 9a9210891..9e0d1f535 100644 --- a/tests/testthat/_snaps/as_kable_extra.md +++ b/tests/testthat/_snaps/as_kable_extra.md @@ -75,7 +75,7 @@ [9] "