Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion R/g_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,11 @@ g_km <- function(df,

# add median survival time annotation table
if (annot_surv_med) {
surv_med_tbl <- h_tbl_median_surv(fit_km = fit_km, armval = armval)
surv_med_tbl <- h_tbl_median_surv(
fit_km = fit_km,
armval = armval,
digits = control_annot_surv_med[["digits"]] %||% 4
)
bg_fill <- if (isTRUE(control_annot_surv_med[["fill"]])) "#00000020" else control_annot_surv_med[["fill"]]

gg_surv_med <- df2gg(surv_med_tbl, font_size = font_size, colwidths = c(1, 1, 2), bg_fill = bg_fill) +
Expand Down
21 changes: 16 additions & 5 deletions R/h_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@
#' table can be added in [g_km()] by setting `annot_surv_med=TRUE`, and can be configured using the
#' `control_surv_med_annot()` function by setting it as the `control_annot_surv_med` argument.
#'
#' @param digits (`integer(1)`)\cr number of significant digits to use for rounding the
#' median survival time estimates and confidence interval values in the annotation table. Defaults to `4`.
#'
#' @examples
#' control_surv_med_annot()
#' control_surv_med_annot(digits = 2)
#'
#' @export
control_surv_med_annot <- function(x = 0.8, y = 0.85, w = 0.32, h = 0.16, fill = TRUE) {
control_surv_med_annot <- function(x = 0.8, y = 0.85, w = 0.32, h = 0.16, fill = TRUE, digits = 4) {
assert_proportion_value(x)
assert_proportion_value(y)
assert_proportion_value(w)
assert_proportion_value(h)
checkmate::assert_int(digits, lower = 1)

list(x = x, y = y, w = w, h = h, fill = fill)
list(x = x, y = y, w = w, h = h, fill = fill, digits = digits)
}

#' @describeIn control_annot Control function for formatting the Cox-PH annotation table. This annotation table can be
Expand Down Expand Up @@ -117,6 +122,8 @@
#' Transform a survival fit to a table with groups in rows characterized by N, median and confidence interval.
#'
#' @inheritParams h_data_plot
#' @param digits (`integer(1)`)\cr number of significant digits to use for rounding the
#' median survival time estimates and confidence interval values. Defaults to `4`.
#'
#' @return A summary table with statistics `N`, `Median`, and `XX% CI` (`XX` taken from `fit_km`).
#'
Expand All @@ -130,9 +137,12 @@
#' data = adtte
#' )
#' h_tbl_median_surv(fit_km = fit)
#' h_tbl_median_surv(fit_km = fit, digits = 2)
#'
#' @export
h_tbl_median_surv <- function(fit_km, armval = "All") {
h_tbl_median_surv <- function(fit_km, armval = "All", digits = 4) {
checkmate::assert_int(digits, lower = 1)

y <- if (is.null(fit_km$strata)) {
as.data.frame(t(summary(fit_km)$table), row.names = armval)
} else {
Expand All @@ -141,11 +151,12 @@
rownames(tbl) <- matrix(unlist(rownames_lst), ncol = 2, byrow = TRUE)[, 2]
as.data.frame(tbl)
}

conf.int <- summary(fit_km)$conf.int # nolint
y$records <- round(y$records)
y$median <- signif(y$median, 4)
y$median <- signif(y$median, digits)
y$`CI` <- paste0(
"(", signif(y[[paste0(conf.int, "LCL")]], 4), ", ", signif(y[[paste0(conf.int, "UCL")]], 4), ")"
"(", signif(y[[paste0(conf.int, "LCL")]], digits), ", ", signif(y[[paste0(conf.int, "UCL")]], digits), ")"
)
stats::setNames(
y[c("records", "median", "CI")],
Expand Down Expand Up @@ -1109,7 +1120,7 @@
"Warning: Cox table will not be displayed as there is",
"not any level to be compared in the arm variable."
))
return(

Check warning on line 1123 in R/h_km.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/h_km.R,line=1123,col=7,[return_linter] Use implicit return behavior; explicit return() is not needed.
grid::gList(
grid::gTree(
vp = NULL,
Expand Down
13 changes: 12 additions & 1 deletion man/control_annot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/h_tbl_median_surv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-h_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ testthat::test_that("control_surv_med_annot works with default settings", {
testthat::expect_snapshot(res)
})

testthat::test_that("control_surv_med_annot works with custom digits", {
result <- control_surv_med_annot(digits = 2)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

testthat::test_that("control_coxph_annot works with default settings", {
result <- control_coxph_annot()

Expand Down Expand Up @@ -84,6 +91,13 @@ testthat::test_that("h_tbl_median_surv estimates median survival time with CI",
testthat::expect_snapshot(res)
})

testthat::test_that("h_tbl_median_surv respects digits parameter", {
result <- h_tbl_median_surv(fit_km = test_fit, digits = 2)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

testthat::test_that("h_tbl_coxph_pairwise estimates HR, CI and pvalue", {
df <- tern_ex_adtte %>%
filter(PARAMCD == "OS") %>%
Expand Down
Loading