diff --git a/NAMESPACE b/NAMESPACE index 4839c16c..3ad66cd6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,9 +1,9 @@ # Generated by roxygen2: do not edit by hand -S3method(as_gt,fixed_design) +S3method(as_gt,fixed_design_summary) S3method(as_gt,gs_design) S3method(as_gt,simtrial_gs_wlr) -S3method(as_rtf,fixed_design) +S3method(as_rtf,fixed_design_summary) S3method(as_rtf,gs_design) S3method(summary,fixed_design) S3method(summary,gs_design) diff --git a/R/as_gt.R b/R/as_gt.R index f22a39f2..724ffb69 100644 --- a/R/as_gt.R +++ b/R/as_gt.R @@ -78,12 +78,14 @@ as_gt <- function(x, ...) { #' ) %>% #' summary() %>% #' as_gt() -as_gt.fixed_design <- function(x, title = NULL, footnote = NULL, ...) { - method <- fd_method(x) +as_gt.fixed_design_summary <- function(x, title = NULL, footnote = NULL, ...) { + if (is.null(title)) title <- attr(x, "title") + if (is.null(footnote)) footnote <- attr(x, "footnote") + ans <- gt::gt(x) %>% - gt::tab_header(title = title %||% fd_title(method)) %>% + gt::tab_header(title = title) %>% gt::tab_footnote( - footnote = footnote %||% fd_footnote(x, method), + footnote = footnote, locations = gt::cells_title(group = "title") ) return(ans) @@ -91,48 +93,6 @@ as_gt.fixed_design <- function(x, title = NULL, footnote = NULL, ...) { get_method <- function(x, methods) intersect(methods, class(x))[1] -# get the fixed design method -fd_method <- function(x) { - get_method(x, c("ahr", "fh", "mb", "lf", "rd", "maxcombo", "milestone", "rmst")) -} - -# get the default title -fd_title <- function(method) { - sprintf("Fixed Design %s Method", switch( - method, - ahr = "under AHR", fh = "under Fleming-Harrington", mb = "under Magirr-Burman", - lf = "under Lachin and Foulkes", maxcombo = "under MaxCombo", - milestone = "under Milestone", rmst = "under Restricted Mean Survival Time", - rd = "of Risk Difference under Farrington-Manning" - )) -} - -# get the default footnote -fd_footnote <- function(x, method) { - switch( - method, - ahr = "Power computed with average hazard ratio method.", - fh = paste( - "Power for Fleming-Harrington test", substring(x$Design, 19), - "using method of Yung and Liu." - ), - lf = paste( - "Power using Lachin and Foulkes method applied using expected", - "average hazard ratio (AHR) at time of planned analysis." - ), - rd = paste( - "Risk difference power without continuity correction using method of", - "Farrington and Manning." - ), - maxcombo = paste0( - "Power for MaxCombo test with Fleming-Harrington tests ", - substring(x$Design, 9), "." - ), - # for mb, milestone, and rmst - paste("Power for", x$Design, "computed with method of Yung and Liu.") - ) -} - #' @rdname as_gt #' #' @param title A string to specify the title of the gt table. diff --git a/R/as_rtf.R b/R/as_rtf.R index 035182ea..468187c3 100644 --- a/R/as_rtf.R +++ b/R/as_rtf.R @@ -91,7 +91,7 @@ as_rtf <- function(x, ...) { #' ) %>% #' summary() %>% #' as_rtf(file = tempfile(fileext = ".rtf")) -as_rtf.fixed_design <- function( +as_rtf.fixed_design_summary <- function( x, title = NULL, footnote = NULL, @@ -101,9 +101,10 @@ as_rtf.fixed_design <- function( file, ...) { orientation <- match.arg(orientation) - method <- fd_method(x) - title <- title %||% paste(fd_title(method), "{^a}") - footnote <- footnote %||% paste("{^a}", fd_footnote(x, method)) + if (is.null(title)) title <- attr(x, "title") + if (is.null(footnote)) footnote <- attr(x, "footnote") + title <- paste(title, "{^a}") + footnote <- paste("{^a}", footnote) # set default column width n_row <- nrow(x) diff --git a/R/fixed_design_ahr.R b/R/fixed_design_ahr.R index 888f9900..9a4b7ac9 100644 --- a/R/fixed_design_ahr.R +++ b/R/fixed_design_ahr.R @@ -119,6 +119,8 @@ fixed_design_ahr <- function( analysis_time = study_duration ) } + + # Prepare output ---- ans <- tibble( design = "ahr", n = d$analysis$n, @@ -128,10 +130,16 @@ fixed_design_ahr <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, enroll_rate = d$enroll_rate, - fail_rate = d$fail_rate, analysis = ans, design = "ahr" + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "ahr" + ), + class = "fixed_design", + design_display = "Average hazard ratio", + title = "Fixed Design under AHR Method", + footnote = "Power computed with average hazard ratio method." ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_fh.R b/R/fixed_design_fh.R index d65ba827..c1eaee9a 100644 --- a/R/fixed_design_fh.R +++ b/R/fixed_design_fh.R @@ -118,6 +118,8 @@ fixed_design_fh <- function( analysis_time = study_duration ) } + + # Prepare output ---- ans <- tibble( design = "fh", n = d$analysis$n, @@ -127,11 +129,23 @@ fixed_design_fh <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, - analysis = ans, - design = "fh", design_par = list(rho = rho, gamma = gamma) + design_display <- paste0( + "Fleming-Harrington FH(", rho, ", ", gamma, ")", + if (rho == 0 && gamma == 0) " (logrank)" + ) + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "fh", design_par = list(rho = rho, gamma = gamma) + ), + class = "fixed_design", + design_display = design_display, + title = "Fixed Design under Fleming-Harrington Method", + footnote = paste( + "Power for Fleming-Harrington test", substring(design_display, 19), + "using method of Yung and Liu." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_lf.R b/R/fixed_design_lf.R index ede0fce8..250df876 100644 --- a/R/fixed_design_lf.R +++ b/R/fixed_design_lf.R @@ -173,6 +173,8 @@ fixed_design_lf <- function( rr[, 1] }) ) + + # Prepare output ---- ans <- tibble( design = "lf", n = d$n, @@ -182,13 +184,22 @@ fixed_design_lf <- function( alpha = d$alpha, power = d$power ) - y <- list( - input = input, - enroll_rate = enroll_rate %>% mutate(rate = rate * d$n / sum(enroll_rate$duration * enroll_rate$rate)), - fail_rate = fail_rate, - analysis = ans, - design = "lf" + y <- structure( + list( + input = input, + enroll_rate = enroll_rate %>% mutate(rate = rate * d$n / sum(enroll_rate$duration * enroll_rate$rate)), + fail_rate = fail_rate, + analysis = ans, + design = "lf" + ), + class = "fixed_design", + design_display = "Lachin and Foulkes", + title = "Fixed Design under Lachin and Foulkes Method", + footnote = paste( + "Power using Lachin and Foulkes method applied using expected", + "average hazard ratio (AHR) at time of planned analysis." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_maxcombo.R b/R/fixed_design_maxcombo.R index 0475f513..f9df5142 100644 --- a/R/fixed_design_maxcombo.R +++ b/R/fixed_design_maxcombo.R @@ -117,7 +117,8 @@ fixed_design_maxcombo <- function( lower = gs_b, lpar = -Inf ) } - # get the output of MaxCombo + + # Prepare output ---- ans <- tibble( design = "maxcombo", n = d$analysis$n, @@ -127,11 +128,30 @@ fixed_design_maxcombo <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, - enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, analysis = ans, - design = "maxcombo", design_par = list(rho = rho, gamma = gamma, tau = tau) + design_display <- gsub( + "FH(0, 0)", "logrank", paste( + "MaxCombo:", paste0( + "FHC(", rho, ", ", gamma, ")", + collapse = ", " + ) + ), + fixed = TRUE + ) + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "maxcombo", + design_par = list(rho = rho, gamma = gamma, tau = tau) + ), + class = "fixed_design", + design_display = design_display, + title = "Fixed Design under MaxCombo Method", + footnote = paste0( + "Power for MaxCombo test with Fleming-Harrington tests ", + substring(design_display, 9), + "." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_mb.R b/R/fixed_design_mb.R index b79cb9d5..db5101b3 100644 --- a/R/fixed_design_mb.R +++ b/R/fixed_design_mb.R @@ -117,7 +117,8 @@ fixed_design_mb <- function( analysis_time = study_duration ) } - # get the output of MB + + # Prepare output ---- ans <- tibble( design = "mb", n = d$analysis$n, @@ -127,10 +128,20 @@ fixed_design_mb <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, analysis = ans, - design = "mb", design_par = list(tau = tau) + design_display <- paste0("Modestly weighted LR: tau = ", tau) + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "mb", design_par = list(tau = tau) + ), + class = "fixed_design", + design_display = design_display, + title = "Fixed Design under Magirr-Burman Method", + footnote = paste( + "Power for", design_display, + "computed with method of Yung and Liu." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_milestone.R b/R/fixed_design_milestone.R index 3ccfee34..91d43faa 100644 --- a/R/fixed_design_milestone.R +++ b/R/fixed_design_milestone.R @@ -105,7 +105,8 @@ fixed_design_milestone <- function( tau = tau ) } - # get the output of MaxCombo + + # Prepare output ---- ans <- tibble( design = "milestone", n = d$analysis$n, @@ -115,11 +116,20 @@ fixed_design_milestone <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, - enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, analysis = ans, - design = "milestone", design_par = list(tau = tau) + design_display <- paste("Milestone: tau =", tau) + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "milestone", design_par = list(tau = tau) + ), + class = "fixed_design", + design_display = design_display, + title = "Fixed Design under Milestone Method", + footnote = paste( + "Power for", design_display, + "computed with method of Yung and Liu." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_rd.R b/R/fixed_design_rd.R index 171d2d44..ed780202 100644 --- a/R/fixed_design_rd.R +++ b/R/fixed_design_rd.R @@ -94,7 +94,8 @@ fixed_design_rd <- function( rd0 = rd0, weight = "unstratified" ) } - # get the output of MaxCombo + + # Prepare output ---- ans <- tibble( design = "rd", n = d$analysis$n, @@ -102,10 +103,19 @@ fixed_design_rd <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, - enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, analysis = ans, design = "rd" + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "rd" + ), + class = "fixed_design", + design_display = "Risk difference", + title = "Fixed Design of Risk Difference under Farrington-Manning Method", + footnote = paste( + "Risk difference power without continuity correction using method of", + "Farrington and Manning." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/fixed_design_rmst.R b/R/fixed_design_rmst.R index 340a500a..e7dc9a59 100644 --- a/R/fixed_design_rmst.R +++ b/R/fixed_design_rmst.R @@ -104,7 +104,8 @@ fixed_design_rmst <- function( tau = tau ) } - # get the output + + # Prepare output ---- ans <- tibble( design = "rmst", n = d$analysis$n, @@ -114,11 +115,21 @@ fixed_design_rmst <- function( alpha = alpha, power = (d$bound %>% filter(bound == "upper"))$probability ) - y <- list( - input = input, - enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, analysis = ans, - design = "rmst", design_par = list(tau = tau), study_duration + design_display <- paste("RMST: tau =", tau) + y <- structure( + list( + input = input, enroll_rate = d$enroll_rate, fail_rate = d$fail_rate, + analysis = ans, design = "rmst", design_par = list(tau = tau), + study_duration + ), + class = "fixed_design", + design_display = design_display, + title = "Fixed Design under Restricted Mean Survival Time Method", + footnote = paste( + "Power for", design_display, + "computed with method of Yung and Liu." + ) ) - class(y) <- c("fixed_design", class(y)) + return(y) } diff --git a/R/summary.R b/R/summary.R index fa36f867..fe57acd2 100644 --- a/R/summary.R +++ b/R/summary.R @@ -78,29 +78,16 @@ #' ) %>% summary() #' summary.fixed_design <- function(object, ...) { - x <- object - p <- x$design_par - ans <- x$analysis - ans$design <- switch( - x$design, - ahr = "Average hazard ratio", - lf = "Lachin and Foulkes", - rd = "Risk difference", - milestone = paste0("Milestone: tau = ", p$tau), - rmst = paste0("RMST: tau = ", p$tau), - mb = paste0("Modestly weighted LR: tau = ", p$tau), - fh = paste0( - "Fleming-Harrington FH(", p$rho, ", ", p$gamma, ")", - if (p$rho == 0 && p$gamma == 0) " (logrank)" - ), - maxcombo = gsub("FH(0, 0)", "logrank", paste( - "MaxCombo:", paste0("FHC(", p[[1]], ", ", p[[2]], ")", collapse = ", ") - ), fixed = TRUE) - ) + ans <- object$analysis + ans$design <- attr(object, "design_display") # capitalize names ans <- cap_names(ans) - ans <- add_class(ans, "fixed_design", x$design) + # Propagate attributes for as_gt()/as_rtf() tables + attr(ans, "title") <- attr(object, "title") + attr(ans, "footnote") <- attr(object, "footnote") + + ans <- add_class(ans, "fixed_design_summary") return(ans) } diff --git a/R/utils.R b/R/utils.R index 5257379b..a7cbb756 100644 --- a/R/utils.R +++ b/R/utils.R @@ -46,3 +46,5 @@ is_wholenumber <- function (x, tol = .Machine$double.eps^0.5) { abs(x - round(x)) < tol } +# Require exact matching by default when retrieving attributes +attr = function(...) base::attr(..., exact = TRUE) diff --git a/_pkgdown.yml b/_pkgdown.yml index 2f9a7bed..f4336921 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -86,10 +86,10 @@ reference: - summary.fixed_design - summary.gs_design - as_gt - - as_gt.fixed_design + - as_gt.fixed_design_summary - as_gt.gs_design - as_rtf - - as_rtf.fixed_design + - as_rtf.fixed_design_summary - as_rtf.gs_design - to_integer - to_integer.fixed_design diff --git a/man/as_gt.Rd b/man/as_gt.Rd index 7b342d85..6d270674 100644 --- a/man/as_gt.Rd +++ b/man/as_gt.Rd @@ -2,13 +2,13 @@ % Please edit documentation in R/as_gt.R \name{as_gt} \alias{as_gt} -\alias{as_gt.fixed_design} +\alias{as_gt.fixed_design_summary} \alias{as_gt.gs_design} \title{Convert summary table of a fixed or group sequential design object to a gt object} \usage{ as_gt(x, ...) -\method{as_gt}{fixed_design}(x, title = NULL, footnote = NULL, ...) +\method{as_gt}{fixed_design_summary}(x, title = NULL, footnote = NULL, ...) \method{as_gt}{gs_design}( x, diff --git a/man/as_rtf.Rd b/man/as_rtf.Rd index b7924f55..bcc85f68 100644 --- a/man/as_rtf.Rd +++ b/man/as_rtf.Rd @@ -2,13 +2,13 @@ % Please edit documentation in R/as_rtf.R \name{as_rtf} \alias{as_rtf} -\alias{as_rtf.fixed_design} +\alias{as_rtf.fixed_design_summary} \alias{as_rtf.gs_design} \title{Write summary table of a fixed or group sequential design object to an RTF file} \usage{ as_rtf(x, ...) -\method{as_rtf}{fixed_design}( +\method{as_rtf}{fixed_design_summary}( x, title = NULL, footnote = NULL, diff --git a/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_footnote.rtf b/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_footnote.rtf index ab1bf343..d5c194c5 100644 --- a/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_footnote.rtf +++ b/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_footnote.rtf @@ -54,7 +54,7 @@ \intbl\row\pard \trowd\trgaph108\trleft0\trqc \clbrdrl\brdrs\brdrw15\clbrdrt\brdrw15\clbrdrr\brdrs\brdrw15\clbrdrb\brdrdb\brdrw15\clvertalt\cellx9000 -\pard\hyphpar0\sb15\sa15\fi0\li0\ri0\ql\fs18{\f0 Power computed with average hazard ratio method given the sample size}\cell +\pard\hyphpar0\sb15\sa15\fi0\li0\ri0\ql\fs18{\f0 {\super a} Power computed with average hazard ratio method given the sample size}\cell \intbl\row\pard diff --git a/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_title.rtf b/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_title.rtf index 1cb069eb..65790a18 100644 --- a/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_title.rtf +++ b/tests/testthat/_snaps/independent_as_rtf/fixed_design_ahr_title.rtf @@ -17,7 +17,7 @@ \margl1800\margr1440\margt2520\margb1800\headery2520\footery1449 -{\pard\hyphpar\sb180\sa180\fi0\li0\ri0\qc\fs24{\f0 Fixed design under non-proportional hazards}\par} +{\pard\hyphpar\sb180\sa180\fi0\li0\ri0\qc\fs24{\f0 Fixed design under non-proportional hazards {\super a}}\par} \trowd\trgaph108\trleft0\trqc