Skip to content

Commit

Permalink
factor out the code to generate the default title and footnote to be …
Browse files Browse the repository at this point in the history
…reused by both as_gt() and as_rtf()
  • Loading branch information
yihui committed Aug 12, 2024
1 parent 52a5165 commit 235466b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 122 deletions.
40 changes: 24 additions & 16 deletions R/as_gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,37 @@ as_gt <- function(x, ...) {
#' summary() %>%
#' as_gt()
as_gt.fixed_design <- function(x, title = NULL, footnote = NULL, ...) {
# get the design method
design_mtd <- intersect(
c("ahr", "fh", "mb", "lf", "rd", "maxcombo", "milestone", "rmst"), class(x)
)[1]
method <- design_method(x)
ans <- gt::gt(x) %>%
gt::tab_header(title = title %||% method_title(method)) %>%
gt::tab_footnote(
footnote = footnote %||% method_footnote(x, method),
locations = gt::cells_title(group = "title")
)
return(ans)
}

# set the default title
if (is.null(title)) title <- sprintf("Fixed Design %s Method", switch(
design_mtd,
# get the design method
design_method <- function(x) {
methods <- c("ahr", "fh", "mb", "lf", "rd", "maxcombo", "milestone", "rmst")
intersect(methods, class(x))[1]
}

# get the default title
method_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"
))
}

# set the default footnote
if (is.null(footnote)) footnote <- switch(
design_mtd,
# get the default footnote
method_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),
Expand All @@ -116,12 +130,6 @@ as_gt.fixed_design <- function(x, title = NULL, footnote = NULL, ...) {
# for mb, milestone, and rmst
paste("Power for", x$Design, "computed with method of Yung and Liu.")
)

ans <- gt::gt(x) %>%
gt::tab_header(title = title) %>%
gt::tab_footnote(footnote = footnote, locations = gt::cells_title(group = "title"))

return(ans)
}

#' @rdname as_gt
Expand Down
109 changes: 3 additions & 106 deletions R/as_rtf.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,112 +101,9 @@ as_rtf.fixed_design <- function(
file,
...) {
orientation <- match.arg(orientation)

# get the design method
if ("ahr" %in% class(x)) {
design_mtd <- "ahr"
} else if ("fh" %in% class(x)) {
design_mtd <- "fh"
} else if ("mb" %in% class(x)) {
design_mtd <- "mb"
} else if ("lf" %in% class(x)) {
design_mtd <- "lf"
} else if ("rd" %in% class(x)) {
design_mtd <- "rd"
} else if ("maxcombo" %in% class(x)) {
design_mtd <- "maxcombo"
} else if ("milestone" %in% class(x)) {
design_mtd <- "milestone"
} else if ("rmst" %in% class(x)) {
design_mtd <- "rmst"
} else if ("rd" %in% class(x)) {
design_mtd <- "rd"
}

# set the default title
if (is.null(title)) {
title <- switch(design_mtd,
"ahr" = {
paste0("Fixed Design under AHR Method", " {^a}")
},
"fh" = {
paste0("Fixed Design under Fleming-Harrington Method", " {^a}")
},
"mb" = {
paste0("Fixed Design under Magirr-Burman Method", " {^a}")
},
"lf" = {
paste0("Fixed Design under Lachin and Foulkes Method", " {^a}")
},
"rd" = {
paste0("Fixed Design of Risk Difference under Farrington-Manning Method", " {^a}")
},
"maxcombo" = {
paste0("Fixed Design under MaxCombo Method", " {^a}")
},
"milestone" = {
paste0("Fixed Design under Milestone Method", " {^a}")
},
"rmst" = {
paste0("Fixed Design under Restricted Mean Survival Time Method", " {^a}")
},
"rd" = {
paste0("Fixed Design of Risk Difference", " {^a}")
}
)
}


# set the default footnote
if (is.null(footnote)) {
footnote <- switch(design_mtd,
"ahr" = {
paste0("{^a} ", "Power computed with average hazard ratio method.")
},
"fh" = {
paste0(
"{^a} ",
"Power for Fleming-Harrington test ",
substr(x$Design, 19, nchar(x$Design)),
" using method of Yung and Liu."
)
},
"mb" = {
paste0(
"{^a} ",
"Power for ",
x$Design,
" computed with method of Yung and Liu."
)
},
"lf" = {
paste0(
"{^a} ",
"Power using Lachin and Foulkes method applied
using expected average hazard ratio (AHR) at time of planned analysis."
)
},
"rd" = {
paste0(
"{^a} ",
"Risk difference power without continuity correction using method of Farrington and Manning."
)
},
"maxcombo" = {
paste0(
"{^a} ",
"Power for MaxCombo test with Fleming-Harrington tests",
substr(x$Design, 9, nchar(x$Design)), "."
)
},
"milestone" = {
paste0("{^a} ", "Power for ", x$Design, " computed with method of Yung and Liu.")
},
"rmst" = {
paste0("{^a} ", "Power for ", x$Design, " computed with method of Yung and Liu.")
}
)
}
method <- design_method(x)
title <- title %||% paste(method_title(method), "{^a}")
footnote <- footnote %||% paste("{^a}", method_footnote(x, method, footnote))

# set default column width
n_row <- nrow(x)
Expand Down

0 comments on commit 235466b

Please sign in to comment.