From 7fea0019fe136c9e9eaa732dc2e60b22e632801c Mon Sep 17 00:00:00 2001 From: Kai Gu Date: Thu, 9 May 2024 17:25:38 +0800 Subject: [PATCH] add feature of exporting PDF --- R/pkg-methods.R | 18 ++++++++++++++++-- man/s_get_coxph.Rd | 4 +++- man/s_get_survfit.Rd | 4 +++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/R/pkg-methods.R b/R/pkg-methods.R index 6bc5852..87d7eed 100644 --- a/R/pkg-methods.R +++ b/R/pkg-methods.R @@ -242,7 +242,8 @@ print.or_ci <- function(x, ...) { #' @exportS3Method #' @keywords internal #' @param fm (`string`)\cr string of unit for survival time. -print.s_survival <- function(x, fm = "months", ...) { +#' @param export_pdf (`logical`)\cr whether to export PDF to the current directory. +print.s_survival <- function(x, fm = "months", export_pdf = FALSE, ...) { cat("Surv formula: ", x$params$formula, "\n", sep = "") grp <- unique(x$surv$quantile$group) cat("Group by: ", paste(grp, collapse = ", "), "\n", sep = "") @@ -408,13 +409,20 @@ print.s_survival <- function(x, fm = "months", ...) { build_table(df) print(result) + if (export_pdf) { + tf <- tempfile(pattern = "t_surv_", tmpdir = getwd(), fileext = ".pdf") + file <- rtables::export_as_pdf(result, file = tf) + print(file$file) + } + invisible(x) } #' @describeIn s_get_coxph prints survival analysis summary from `coxph`. #' @exportS3Method #' @keywords internal -print.s_coxph <- function(x, ...) { +#' @param export_pdf (`logical`)\cr whether to export PDF to the current directory. +print.s_coxph <- function(x, export_pdf = FALSE, ...) { cat("Surv formula: ", x$params$formula, "\n", sep = "") grp <- x$params$group cat("Group by: ", paste(grp, collapse = ", "), "\n", sep = "") @@ -496,6 +504,12 @@ print.s_coxph <- function(x, ...) { build_table(df) print(result) + if (export_pdf) { + tf <- tempfile(pattern = "t_coxph_", tmpdir = getwd(), fileext = ".pdf") + file <- rtables::export_as_pdf(result, file = tf) + print(file$file) + } + invisible(x) } diff --git a/man/s_get_coxph.Rd b/man/s_get_coxph.Rd index f8f41a5..92a988a 100644 --- a/man/s_get_coxph.Rd +++ b/man/s_get_coxph.Rd @@ -16,7 +16,7 @@ s_get_coxph( ... ) -\method{print}{s_coxph}(x, ...) +\method{print}{s_coxph}(x, export_pdf = FALSE, ...) } \arguments{ \item{data}{(\code{data.frame})\cr a data frame as input.} @@ -38,6 +38,8 @@ Score (logrank) test).} \item{pairwise}{(\code{logical})\cr whether to conduct the pairwise comparison.} \item{...}{other arguments to be passed to \code{\link[survival:coxph]{survival::coxph()}}.} + +\item{export_pdf}{(\code{logical})\cr whether to export PDF to the current directory.} } \value{ An object of class \code{s_coxph} is a list contains hazards ratio and p-value tables. diff --git a/man/s_get_survfit.Rd b/man/s_get_survfit.Rd index 475e9e5..442801f 100644 --- a/man/s_get_survfit.Rd +++ b/man/s_get_survfit.Rd @@ -21,7 +21,7 @@ s_get_survfit( ... ) -\method{print}{s_survival}(x, fm = "months", ...) +\method{print}{s_survival}(x, fm = "months", export_pdf = FALSE, ...) h_pairwise_survdiff(formula, data, strata = NULL, rho = 0) } @@ -56,6 +56,8 @@ can see \code{\link[survival:survdiff]{survival::survdiff()}}.} \item{...}{other arguments to be passed to \code{\link[survival:survfit]{survival::survfit()}}.} \item{fm}{(\code{string})\cr string of unit for survival time.} + +\item{export_pdf}{(\code{logical})\cr whether to export PDF to the current directory.} } \value{ An object of class \code{s_survival} is a list contains several summary tables