From 22b83cd7ae443009c04829b13cde5e374102eddc Mon Sep 17 00:00:00 2001 From: Zelos Zhu Date: Thu, 20 Jun 2024 21:24:22 +0000 Subject: [PATCH 1/3] add tests for add_n, add_nevent, add_p for tbl_survfit --- tests/testthat/test-add_n.tbl_survfit.R | 50 ++++++++++++++++++++ tests/testthat/test-add_nevent.tbl_survfit.R | 48 +++++++++++++++++++ tests/testthat/test-add_p.tbl_survfit.R | 11 +++++ 3 files changed, 109 insertions(+) create mode 100644 tests/testthat/test-add_n.tbl_survfit.R create mode 100644 tests/testthat/test-add_nevent.tbl_survfit.R create mode 100644 tests/testthat/test-add_p.tbl_survfit.R diff --git a/tests/testthat/test-add_n.tbl_survfit.R b/tests/testthat/test-add_n.tbl_survfit.R new file mode 100644 index 000000000..6829d0480 --- /dev/null +++ b/tests/testthat/test-add_n.tbl_survfit.R @@ -0,0 +1,50 @@ +test_that("add_n.tbl_survfit() works", { + tbl <- trial |> + tbl_survfit( + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) + + # total N added to table is accurate + expect_silent( + res <- tbl |> add_n() + ) + + expect_equal( + as.data.frame(res, col_label = FALSE)$N, + c('200', NA, NA) + ) + + # stacked survfits works + fit1 <- survfit(Surv(ttdeath, death) ~ 1, trial) + fit2 <- survfit(Surv(ttdeath, death) ~ grade, trial) + + expect_silent( + res1 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_n() + ) + expect_equal( + as.data.frame(res1, col_label = FALSE)$N, + c('200', '200', NA, NA, NA) + ) + + + # add_n.tbl_survfit does not accept additional arguments + expect_error( + res2 <- tbl |> add_n(statistic = "{N_nonmiss} / {N_obs}"), + regexp = "`...` must be empty" + ) + + # mess with the call object, trigger `safe_survfit_eval` function + trial2 <- NA + fit1$call$data <- trial2 + + expect_error( + res3 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_n(), + regexp = "error may be a due to the construction of the original" + ) +}) diff --git a/tests/testthat/test-add_nevent.tbl_survfit.R b/tests/testthat/test-add_nevent.tbl_survfit.R new file mode 100644 index 000000000..9f3ead804 --- /dev/null +++ b/tests/testthat/test-add_nevent.tbl_survfit.R @@ -0,0 +1,48 @@ +test_that("add_n.tbl_survfit() works", { + tbl <- trial |> + tbl_survfit( + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) + + # total N added to table is accurate + expect_silent( + res <- tbl |> add_nevent() + ) + + expect_equal( + as.data.frame(res, col_label = FALSE)$nevent, + c('112', NA, NA) + ) + + # stacked survfits works + fit1 <- survfit(Surv(ttdeath, death) ~ 1, trial) + fit2 <- survfit(Surv(ttdeath, death) ~ trt, trial) + + expect_silent( + res1 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_nevent() + ) + expect_equal( + as.data.frame(res1, col_label = FALSE)$nevent, + c('112', '112', NA, NA) + ) + + + # add_nevent.tbl_survfit does not accept additional arguments (yet) + expect_error( + res2 <- tbl |> add_nevent(location = "level"), + regexp = "`...` must be empty" + ) + + # mess with the tbl_survfit object to trigger error + res3 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) + class(res3$inputs$x[[1]]) <- NULL + expect_error( + res3 |> add_nevent(), + regexp = "objects must have class" + ) +}) diff --git a/tests/testthat/test-add_p.tbl_survfit.R b/tests/testthat/test-add_p.tbl_survfit.R new file mode 100644 index 000000000..36d4e7daa --- /dev/null +++ b/tests/testthat/test-add_p.tbl_survfit.R @@ -0,0 +1,11 @@ +test_that("add_p.tbl_survfit() works", { + expect_silent( + trial |> + tbl_survfit( + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) |> + add_p() + ) +}) From 6194637daaaac32c31e66dc39d57284cd59ce678 Mon Sep 17 00:00:00 2001 From: Zelos Zhu Date: Fri, 21 Jun 2024 17:52:53 +0000 Subject: [PATCH 2/3] fix all but one --- tests/testthat/test-add_n.tbl_survfit.R | 16 ++++++++-------- tests/testthat/test-add_nevent.tbl_survfit.R | 14 +++++++------- tests/testthat/test-add_p.tbl_survfit.R | 19 +++++++++++-------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/tests/testthat/test-add_n.tbl_survfit.R b/tests/testthat/test-add_n.tbl_survfit.R index 6829d0480..03ef757dd 100644 --- a/tests/testthat/test-add_n.tbl_survfit.R +++ b/tests/testthat/test-add_n.tbl_survfit.R @@ -1,10 +1,10 @@ test_that("add_n.tbl_survfit() works", { tbl <- trial |> tbl_survfit( - include = trt, - y = "Surv(ttdeath, death)", - times = 12 - ) + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) # total N added to table is accurate expect_silent( @@ -13,12 +13,12 @@ test_that("add_n.tbl_survfit() works", { expect_equal( as.data.frame(res, col_label = FALSE)$N, - c('200', NA, NA) + c("200", NA, NA) ) # stacked survfits works - fit1 <- survfit(Surv(ttdeath, death) ~ 1, trial) - fit2 <- survfit(Surv(ttdeath, death) ~ grade, trial) + fit1 <- survival::survfit(survival::Surv(ttdeath, death) ~ 1, trial) + fit2 <- survival::survfit(survival::Surv(ttdeath, death) ~ grade, trial) expect_silent( res1 <- list(fit1, fit2) |> @@ -27,7 +27,7 @@ test_that("add_n.tbl_survfit() works", { ) expect_equal( as.data.frame(res1, col_label = FALSE)$N, - c('200', '200', NA, NA, NA) + c("200", "200", NA, NA, NA) ) diff --git a/tests/testthat/test-add_nevent.tbl_survfit.R b/tests/testthat/test-add_nevent.tbl_survfit.R index 9f3ead804..95b626990 100644 --- a/tests/testthat/test-add_nevent.tbl_survfit.R +++ b/tests/testthat/test-add_nevent.tbl_survfit.R @@ -6,19 +6,19 @@ test_that("add_n.tbl_survfit() works", { times = 12 ) - # total N added to table is accurate + # total N events added to table is accurate expect_silent( res <- tbl |> add_nevent() ) expect_equal( as.data.frame(res, col_label = FALSE)$nevent, - c('112', NA, NA) + c("112", NA, NA) ) # stacked survfits works - fit1 <- survfit(Surv(ttdeath, death) ~ 1, trial) - fit2 <- survfit(Surv(ttdeath, death) ~ trt, trial) + fit1 <- survival::survfit(survival::Surv(ttdeath, death) ~ 1, trial) + fit2 <- survival::survfit(survival::Surv(ttdeath, death) ~ trt, trial) expect_silent( res1 <- list(fit1, fit2) |> @@ -27,7 +27,7 @@ test_that("add_n.tbl_survfit() works", { ) expect_equal( as.data.frame(res1, col_label = FALSE)$nevent, - c('112', '112', NA, NA) + c("112", "112", NA, NA) ) @@ -38,8 +38,8 @@ test_that("add_n.tbl_survfit() works", { ) # mess with the tbl_survfit object to trigger error - res3 <- list(fit1, fit2) |> - tbl_survfit(times = c(12, 24)) + res3 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) class(res3$inputs$x[[1]]) <- NULL expect_error( res3 |> add_nevent(), diff --git a/tests/testthat/test-add_p.tbl_survfit.R b/tests/testthat/test-add_p.tbl_survfit.R index 36d4e7daa..d2f76aec9 100644 --- a/tests/testthat/test-add_p.tbl_survfit.R +++ b/tests/testthat/test-add_p.tbl_survfit.R @@ -1,11 +1,14 @@ test_that("add_p.tbl_survfit() works", { - expect_silent( - trial |> - tbl_survfit( - include = trt, - y = "Surv(ttdeath, death)", - times = 12 - ) |> - add_p() + tbl <- trial |> + tbl_survfit( + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) + + # total N added to table is accurate + expect_error( + res <- tbl |> add_p(), + NA ) }) From 46f7a13d506ba078e542d0f5c013e00cd03e1171 Mon Sep 17 00:00:00 2001 From: Zelos Zhu Date: Fri, 21 Jun 2024 18:05:26 +0000 Subject: [PATCH 3/3] get around errors --- tests/testthat/test-add_n.tbl_survfit.R | 23 ++++---------------- tests/testthat/test-add_nevent.tbl_survfit.R | 2 +- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-add_n.tbl_survfit.R b/tests/testthat/test-add_n.tbl_survfit.R index 03ef757dd..890886087 100644 --- a/tests/testthat/test-add_n.tbl_survfit.R +++ b/tests/testthat/test-add_n.tbl_survfit.R @@ -1,22 +1,5 @@ test_that("add_n.tbl_survfit() works", { - tbl <- trial |> - tbl_survfit( - include = trt, - y = "Surv(ttdeath, death)", - times = 12 - ) - - # total N added to table is accurate - expect_silent( - res <- tbl |> add_n() - ) - - expect_equal( - as.data.frame(res, col_label = FALSE)$N, - c("200", NA, NA) - ) - - # stacked survfits works + # add_n.tbl_survfit works fit1 <- survival::survfit(survival::Surv(ttdeath, death) ~ 1, trial) fit2 <- survival::survfit(survival::Surv(ttdeath, death) ~ grade, trial) @@ -33,7 +16,9 @@ test_that("add_n.tbl_survfit() works", { # add_n.tbl_survfit does not accept additional arguments expect_error( - res2 <- tbl |> add_n(statistic = "{N_nonmiss} / {N_obs}"), + res2 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_n(statistic = "{N_nonmiss} / {N_obs}"), regexp = "`...` must be empty" ) diff --git a/tests/testthat/test-add_nevent.tbl_survfit.R b/tests/testthat/test-add_nevent.tbl_survfit.R index 95b626990..78d5aa5a8 100644 --- a/tests/testthat/test-add_nevent.tbl_survfit.R +++ b/tests/testthat/test-add_nevent.tbl_survfit.R @@ -16,7 +16,7 @@ test_that("add_n.tbl_survfit() works", { c("112", NA, NA) ) - # stacked survfits works + # stacked fits work fit1 <- survival::survfit(survival::Surv(ttdeath, death) ~ 1, trial) fit2 <- survival::survfit(survival::Surv(ttdeath, death) ~ trt, trial)