Skip to content

Commit 1b7a759

Browse files
committed
Add digits argument to h_tbl_median_surv and control_surv_med_annot
Expose digits parameter (default 4) to control signif() precision for median survival time and CI values in the annotation table. The parameter is passed through from g_km() via control_annot_surv_med.
1 parent 1ee65c3 commit 1b7a759

8 files changed

Lines changed: 92 additions & 9 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Enhancements
77
* Added `alternative` argument to `s_coxph_pairwise()` to allow one-sided hypothesis testing.
88
* Added `lr_stat_df` to the parameters return list of `s_coxph_pairwise()`.
9+
* Added `digits` argument to `h_tbl_median_surv()` and `control_surv_med_annot()` to control significant digits for median survival annotation. (#1467)
910

1011
# tern 0.9.10
1112

R/g_km.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,11 @@ g_km <- function(df,
536536

537537
# add median survival time annotation table
538538
if (annot_surv_med) {
539-
surv_med_tbl <- h_tbl_median_surv(fit_km = fit_km, armval = armval)
539+
surv_med_tbl <- h_tbl_median_surv(
540+
fit_km = fit_km,
541+
armval = armval,
542+
digits = control_annot_surv_med[["digits"]] %||% 4
543+
)
540544
bg_fill <- if (isTRUE(control_annot_surv_med[["fill"]])) "#00000020" else control_annot_surv_med[["fill"]]
541545

542546
gg_surv_med <- df2gg(surv_med_tbl, font_size = font_size, colwidths = c(1, 1, 2), bg_fill = bg_fill) +

R/h_km.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ NULL
2323
#' table can be added in [g_km()] by setting `annot_surv_med=TRUE`, and can be configured using the
2424
#' `control_surv_med_annot()` function by setting it as the `control_annot_surv_med` argument.
2525
#'
26+
#' @param digits (`integer(1)`)\cr number of significant digits for median survival
27+
#' time and confidence interval values. Defaults to `4`.
28+
#'
2629
#' @examples
2730
#' control_surv_med_annot()
31+
#' control_surv_med_annot(digits = 2)
2832
#'
2933
#' @export
30-
control_surv_med_annot <- function(x = 0.8, y = 0.85, w = 0.32, h = 0.16, fill = TRUE) {
34+
control_surv_med_annot <- function(x = 0.8, y = 0.85, w = 0.32, h = 0.16, fill = TRUE, digits = 4) {
3135
assert_proportion_value(x)
3236
assert_proportion_value(y)
3337
assert_proportion_value(w)
3438
assert_proportion_value(h)
39+
checkmate::assert_int(digits, lower = 1)
3540

36-
list(x = x, y = y, w = w, h = h, fill = fill)
41+
list(x = x, y = y, w = w, h = h, fill = fill, digits = digits)
3742
}
3843

3944
#' @describeIn control_annot Control function for formatting the Cox-PH annotation table. This annotation table can be
@@ -117,6 +122,8 @@ h_xticks <- function(data, xticks = NULL, max_time = NULL) {
117122
#' Transform a survival fit to a table with groups in rows characterized by N, median and confidence interval.
118123
#'
119124
#' @inheritParams h_data_plot
125+
#' @param digits (`integer(1)`)\cr number of significant digits for median and CI values.
126+
#' Defaults to `4`.
120127
#'
121128
#' @return A summary table with statistics `N`, `Median`, and `XX% CI` (`XX` taken from `fit_km`).
122129
#'
@@ -130,9 +137,11 @@ h_xticks <- function(data, xticks = NULL, max_time = NULL) {
130137
#' data = adtte
131138
#' )
132139
#' h_tbl_median_surv(fit_km = fit)
140+
#' h_tbl_median_surv(fit_km = fit, digits = 2)
133141
#'
134142
#' @export
135-
h_tbl_median_surv <- function(fit_km, armval = "All") {
143+
h_tbl_median_surv <- function(fit_km, armval = "All", digits = 4) {
144+
checkmate::assert_int(digits, lower = 1)
136145
y <- if (is.null(fit_km$strata)) {
137146
as.data.frame(t(summary(fit_km)$table), row.names = armval)
138147
} else {
@@ -143,9 +152,9 @@ h_tbl_median_surv <- function(fit_km, armval = "All") {
143152
}
144153
conf.int <- summary(fit_km)$conf.int # nolint
145154
y$records <- round(y$records)
146-
y$median <- signif(y$median, 4)
155+
y$median <- signif(y$median, digits)
147156
y$`CI` <- paste0(
148-
"(", signif(y[[paste0(conf.int, "LCL")]], 4), ", ", signif(y[[paste0(conf.int, "UCL")]], 4), ")"
157+
"(", signif(y[[paste0(conf.int, "LCL")]], digits), ", ", signif(y[[paste0(conf.int, "UCL")]], digits), ")"
149158
)
150159
stats::setNames(
151160
y[c("records", "median", "CI")],

man/control_annot.Rd

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/h_tbl_median_surv.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tern-package.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/h_km.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@
1818
$fill
1919
[1] TRUE
2020
21+
$digits
22+
[1] 4
23+
24+
25+
# control_surv_med_annot works with custom digits
26+
27+
Code
28+
res
29+
Output
30+
$x
31+
[1] 0.8
32+
33+
$y
34+
[1] 0.85
35+
36+
$w
37+
[1] 0.32
38+
39+
$h
40+
[1] 0.16
41+
42+
$fill
43+
[1] TRUE
44+
45+
$digits
46+
[1] 2
47+
2148

2249
# control_coxph_annot works with default settings
2350

@@ -39,6 +66,9 @@
3966
$fill
4067
[1] TRUE
4168
69+
$digits
70+
[1] 4
71+
4272
$ref_lbls
4373
[1] FALSE
4474
@@ -98,6 +128,16 @@
98128
ARM B 73 727.8 (555.8, 1156)
99129
ARM C 58 632.3 (393, 1001)
100130

131+
# h_tbl_median_surv respects digits parameter
132+
133+
Code
134+
res
135+
Output
136+
N Median 95% CI
137+
ARM A 69 970 (690, 1600)
138+
ARM B 73 730 (560, 1200)
139+
ARM C 58 630 (390, 1000)
140+
101141
# h_tbl_coxph_pairwise estimates HR, CI and pvalue
102142

103143
Code

tests/testthat/test-h_km.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ testthat::test_that("control_surv_med_annot works with default settings", {
1111
testthat::expect_snapshot(res)
1212
})
1313

14+
testthat::test_that("control_surv_med_annot works with custom digits", {
15+
result <- control_surv_med_annot(digits = 2)
16+
17+
res <- testthat::expect_silent(result)
18+
testthat::expect_snapshot(res)
19+
})
20+
1421
testthat::test_that("control_coxph_annot works with default settings", {
1522
result <- control_coxph_annot()
1623

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

94+
testthat::test_that("h_tbl_median_surv respects digits parameter", {
95+
result <- h_tbl_median_surv(fit_km = test_fit, digits = 2)
96+
97+
res <- testthat::expect_silent(result)
98+
testthat::expect_snapshot(res)
99+
})
100+
87101
testthat::test_that("h_tbl_coxph_pairwise estimates HR, CI and pvalue", {
88102
df <- tern_ex_adtte %>%
89103
filter(PARAMCD == "OS") %>%

0 commit comments

Comments
 (0)