From 278f0bb727fc89ecb579e88d3d390a55e2d4d78e Mon Sep 17 00:00:00 2001 From: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:31:12 -0500 Subject: [PATCH] migrate export_as_pdf (#798) * move remaining powerpoints inside the repo * migrate export_as_pdf * remove grid from imports * Update news file * update formatters version --- DESCRIPTION | 3 +- NAMESPACE | 15 +--- NEWS.md | 1 + R/tt_export.R | 167 ++----------------------------------------- man/export_as_pdf.Rd | 134 ---------------------------------- man/reexports.Rd | 16 ++++- 6 files changed, 24 insertions(+), 312 deletions(-) delete mode 100644 man/export_as_pdf.Rd diff --git a/DESCRIPTION b/DESCRIPTION index f66e427b4..200ddf3f9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,13 +29,12 @@ URL: https://github.com/insightsengineering/rtables, https://insightsengineering.github.io/rtables/ BugReports: https://github.com/insightsengineering/rtables/issues Depends: - formatters (>= 0.5.4.9003), + formatters (>= 0.5.4.9008), magrittr (>= 1.5), methods, R (>= 2.10) Imports: checkmate (>= 2.1.0), - grid, htmltools (>= 0.5.4), stats, stringi (>= 1.6) diff --git a/NAMESPACE b/NAMESPACE index b0ccfe48d..4e35c5200 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -284,21 +284,8 @@ exportMethods(value_at) import(formatters) import(methods) importFrom(formatters,do_forced_paginate) +importFrom(formatters,export_as_pdf) importFrom(formatters,export_as_txt) -importFrom(grDevices,dev.off) -importFrom(grDevices,pdf) -importFrom(grid,convertHeight) -importFrom(grid,convertWidth) -importFrom(grid,get.gpar) -importFrom(grid,gpar) -importFrom(grid,grid.draw) -importFrom(grid,grid.newpage) -importFrom(grid,grobHeight) -importFrom(grid,grobWidth) -importFrom(grid,plotViewport) -importFrom(grid,pushViewport) -importFrom(grid,textGrob) -importFrom(grid,unit) importFrom(htmltools,tagList) importFrom(htmltools,tags) importFrom(magrittr,"%>%") diff --git a/NEWS.md b/NEWS.md index a45f650a3..20c67c3fe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,7 @@ * Added `na_str` argument to `analyze_colvars` to set custom string to print in place of missing values. * Added flat `data.frame` outputs for `as_result_df()` via flag parameters `as_viewer`, `as_strings`, and `expand_colnames`. + * Migrated `export_as_pdf` function to `formatters`. ### Bug Fixes * Fixed a bug that was failing when wrapping and section dividers were used at the same time. diff --git a/R/tt_export.R b/R/tt_export.R index 709c520f4..f149d3122 100644 --- a/R/tt_export.R +++ b/R/tt_export.R @@ -445,44 +445,10 @@ collapse_values <- function(colvals) { # pdf output ------------------------------------------------------------------- #' Export as PDF -#' -#' The PDF output is based on the ASCII output created with `toString` -#' -#' @inheritParams formatters::export_as_txt -#' @inheritParams tostring -#' @inheritParams grid::plotViewport -#' @inheritParams paginate_table -#' @param file file to write, must have `.pdf` extension -#' @param width Deprecated, please use `pg_width` or specify -#' `page_type`. The width of the graphics region in inches -#' @param height Deprecated, please use `pg_height` or specify -#' `page_type`. The height of the graphics region in -#' inches -#' @param fontsize Deprecated, please use `font_size`. the size of -#' text (in points) -#' @param margins numeric(4). The number of lines/characters of margin on the -#' bottom, left, top, and right sides of the page. -#' @param ... arguments passed on to `paginate_table` -#' -#' @importFrom grDevices pdf -#' @importFrom grid textGrob grid.newpage gpar pushViewport plotViewport unit grid.draw -#' convertWidth convertHeight grobHeight grobWidth -#' -#' @details By default, pagination is performed, with default -#' `cpp` and `lpp` defined by specified page dimensions and margins. -#' User-specified `lpp` and `cpp` values override this, and should -#' be used with caution. -#' -#' Title and footer materials are also word-wrapped by default -#' (unlike when printed to the terminal), with `cpp`, as -#' defined above, as the default `max_width`. -#' -#' @seealso [formatters::export_as_txt()] -#' -#' -#' @importFrom grid textGrob get.gpar -#' @importFrom grDevices dev.off -#' @export + +### Migrated to formatters. + +#' @importFrom formatters export_as_pdf #' #' @examples #' lyt <- basic_table() %>% @@ -498,128 +464,8 @@ collapse_values <- function(colvals) { #' export_as_pdf(tbl, file = tf, lpp = 8) #' } #' -export_as_pdf <- function(tt, - file, - page_type = "letter", - landscape = FALSE, - pg_width = page_dim(page_type)[if (landscape) 2 else 1], - pg_height = page_dim(page_type)[if (landscape) 1 else 2], - width = NULL, - height = NULL, # passed to pdf() - margins = c(4, 4, 4, 4), - font_family = "Courier", - fontsize = 8, # grid parameters - font_size = fontsize, - paginate = TRUE, - lpp = NULL, - cpp = NULL, - hsep = "-", - indent_size = 2, - tf_wrap = TRUE, - max_width = NULL, - colwidths = propose_column_widths(matrix_form(tt, TRUE)), - ...) { # passed to paginate_table - stopifnot(file_ext(file) != ".pdf") - if (!is.null(colwidths) && length(colwidths) != ncol(tt) + 1) { - stop( - "non-null colwidths argument must have length ncol(tt) + 1 [", - ncol(tt) + 1, "], got length ", length(colwidths) - ) - } - - gp_plot <- gpar(fontsize = font_size, fontfamily = font_family) - - ## soft deprecation. To become hard deprecation. - if (!is.null(height)) { - pg_height <- height - } - - if (!is.null(width)) { - pg_width <- width - } - - if (missing(font_size) && !missing(fontsize)) { - font_size <- fontsize - } - - pdf(file = file, width = pg_width, height = pg_height) - on.exit(dev.off()) - grid.newpage() - pushViewport(plotViewport(margins = margins, gp = gp_plot)) - - cur_gpar <- get.gpar() - if (is.null(lpp)) { - lpp <- floor( - convertHeight(unit(1, "npc"), "lines", valueOnly = TRUE) / (cur_gpar$cex * cur_gpar$lineheight) - ) - sum(margins[c(1, 3)]) # bottom, top - } - if (is.null(cpp)) { - cpp <- floor( - convertWidth(unit(1, "npc"), "inches", valueOnly = TRUE) * - font_lcpi(font_family, font_size, cur_gpar$lineheight)$cpi - ) - sum(margins[c(2, 4)]) # left, right - } - if (tf_wrap && is.null(max_width)) { - max_width <- cpp - } - - tbls <- if (paginate) { - paginate_table(tt, - lpp = lpp, cpp = cpp, tf_wrap = tf_wrap, max_width = max_width, - colwidths = colwidths, ... - ) - } else { - list(tt) - } - stbls <- lapply(lapply( - tbls, - function(tbl_i) { - cinds <- c(1, .figure_out_colinds(tbl_i, tt) + 1L) - toString(tbl_i, - widths = colwidths[cinds], hsep = hsep, - indent_size = indent_size, tf_wrap = tf_wrap, - max_width = max_width - ) - } - ), function(xi) substr(xi, 1, nchar(xi) - nchar("\n"))) - gtbls <- lapply(stbls, function(txt) { - textGrob( - label = txt, - x = unit(0, "npc"), y = unit(1, "npc"), - just = c("left", "top") - ) - }) - - npages <- length(gtbls) - exceeds_width <- rep(FALSE, npages) - exceeds_height <- rep(FALSE, npages) - - for (i in seq_along(gtbls)) { - g <- gtbls[[i]] - - if (i > 1) { - grid.newpage() - pushViewport(plotViewport(margins = margins, gp = gp_plot)) - } - - if (convertHeight(grobHeight(g), "inches", valueOnly = TRUE) > - convertHeight(unit(1, "npc"), "inches", valueOnly = TRUE)) { # nolint - exceeds_height[i] <- TRUE - warning("height of page ", i, " exceeds the available space") - } - if (convertWidth(grobWidth(g), "inches", valueOnly = TRUE) > - convertWidth(unit(1, "npc"), "inches", valueOnly = TRUE)) { # nolint - exceeds_width[i] <- TRUE - warning("width of page ", i, " exceeds the available space") - } - - grid.draw(g) - } - list( - file = file, npages = npages, exceeds_width = exceeds_width, exceeds_height = exceeds_height, - lpp = lpp, cpp = cpp - ) -} +#' @export +formatters::export_as_pdf # only used in pagination .tab_to_colpath_set <- function(tt) { @@ -635,7 +481,6 @@ export_as_pdf <- function(tt, .tab_to_colpath_set(fulltab) ) } - # Flextable and docx ----------------------------------------------------------- #' Export as word document #' diff --git a/man/export_as_pdf.Rd b/man/export_as_pdf.Rd deleted file mode 100644 index df34fc3c8..000000000 --- a/man/export_as_pdf.Rd +++ /dev/null @@ -1,134 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/tt_export.R -\name{export_as_pdf} -\alias{export_as_pdf} -\title{Export as PDF} -\usage{ -export_as_pdf( - tt, - file, - page_type = "letter", - landscape = FALSE, - pg_width = page_dim(page_type)[if (landscape) 2 else 1], - pg_height = page_dim(page_type)[if (landscape) 1 else 2], - width = NULL, - height = NULL, - margins = c(4, 4, 4, 4), - font_family = "Courier", - fontsize = 8, - font_size = fontsize, - paginate = TRUE, - lpp = NULL, - cpp = NULL, - hsep = "-", - indent_size = 2, - tf_wrap = TRUE, - max_width = NULL, - colwidths = propose_column_widths(matrix_form(tt, TRUE)), - ... -) -} -\arguments{ -\item{tt}{\code{TableTree} (or related class). A \code{TableTree} object representing a -populated table.} - -\item{file}{file to write, must have \code{.pdf} extension} - -\item{page_type}{character(1). Name of a page type. See -\code{page_types}. Ignored when \code{pg_width} and \code{pg_height} -are set directly.} - -\item{landscape}{logical(1). Should the dimensions of \code{page_type} -be inverted for landscape? Defaults to \code{FALSE}, ignored when -\code{pg_width} and \code{pg_height} are set directly.} - -\item{pg_width}{numeric(1). Page width in inches.} - -\item{pg_height}{numeric(1). Page height in inches.} - -\item{width}{Deprecated, please use \code{pg_width} or specify -\code{page_type}. The width of the graphics region in inches} - -\item{height}{Deprecated, please use \code{pg_height} or specify -\code{page_type}. The height of the graphics region in -inches} - -\item{margins}{numeric(4). The number of lines/characters of margin on the -bottom, left, top, and right sides of the page.} - -\item{font_family}{character(1). Name of a font family. An error -will be thrown if the family named is not monospaced. Defaults -to Courier.} - -\item{fontsize}{Deprecated, please use \code{font_size}. the size of -text (in points)} - -\item{font_size}{numeric(1). Font size, defaults to 12.} - -\item{paginate}{logical(1). Whether pagination should be performed, -defaults to \code{TRUE} if page size is specified (including -the default).} - -\item{lpp}{numeric(1) or NULL. Lines per page. if NA (the default, -this is calculated automatically based on the specified page -size). \code{NULL} indicates no vertical pagination should occur.} - -\item{cpp}{numeric(1) or NULL. Width in characters per page. if NA (the default, -this is calculated automatically based on the specified page -size). \code{NULL} indicates no horizontal pagination should occur.} - -\item{hsep}{character(1). Characters to repeat to create -header/body separator line. If \code{NULL}, the object value will be -used. If \code{" "}, an empty separator will be printed. Check \code{\link[formatters:default_hsep]{default_hsep()}} -for more information.} - -\item{indent_size}{numeric(1). Indent size in characters. Ignored -when \code{x} is already a \code{MatrixPrintForm} object in favor of information -there.} - -\item{tf_wrap}{logical(1). Should the texts for title, subtitle, -and footnotes be wrapped?} - -\item{max_width}{integer(1), character(1) or \code{NULL}. Width that title -and footer (including footnotes) materials should be -word-wrapped to. If \code{NULL}, it is set to the current print width -of the session (\code{getOption("width")}). If set to \code{"auto"}, -the width of the table (plus any table inset) is used. Ignored -completely if \code{tf_wrap} is \code{FALSE}.} - -\item{colwidths}{numeric vector. Column widths (in characters) for -use with vertical pagination.} - -\item{...}{arguments passed on to \code{paginate_table}} -} -\description{ -The PDF output is based on the ASCII output created with \code{toString} -} -\details{ -By default, pagination is performed, with default -\code{cpp} and \code{lpp} defined by specified page dimensions and margins. -User-specified \code{lpp} and \code{cpp} values override this, and should -be used with caution. - -Title and footer materials are also word-wrapped by default -(unlike when printed to the terminal), with \code{cpp}, as -defined above, as the default \code{max_width}. -} -\examples{ -lyt <- basic_table() \%>\% - split_cols_by("ARM") \%>\% - analyze(c("AGE", "BMRKR2", "COUNTRY")) - -tbl <- build_table(lyt, ex_adsl) - -\dontrun{ -tf <- tempfile(fileext = ".pdf") -export_as_pdf(tbl, file = tf, pg_height = 4) -tf <- tempfile(fileext = ".pdf") -export_as_pdf(tbl, file = tf, lpp = 8) -} - -} -\seealso{ -\code{\link[formatters:export_as_txt]{formatters::export_as_txt()}} -} diff --git a/man/reexports.Rd b/man/reexports.Rd index b48caffb5..e9b22573e 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -4,6 +4,7 @@ \name{reexports} \alias{reexports} \alias{export_as_txt} +\alias{export_as_pdf} \title{Objects exported from other packages} \examples{ lyt <- basic_table() \%>\% @@ -20,6 +21,19 @@ export_as_txt(tbl, file = tf) system2("cat", tf) } +lyt <- basic_table() \%>\% + split_cols_by("ARM") \%>\% + analyze(c("AGE", "BMRKR2", "COUNTRY")) + +tbl <- build_table(lyt, ex_adsl) + +\dontrun{ +tf <- tempfile(fileext = ".pdf") +export_as_pdf(tbl, file = tf, pg_height = 4) +tf <- tempfile(fileext = ".pdf") +export_as_pdf(tbl, file = tf, lpp = 8) +} + } \keyword{internal} \description{ @@ -27,6 +41,6 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ - \item{formatters}{\code{\link[formatters]{export_as_txt}}} + \item{formatters}{\code{\link[formatters]{export_as_pdf}}, \code{\link[formatters]{export_as_txt}}} }}