Skip to content

Commit

Permalink
migrate export_as_pdf (#798)
Browse files Browse the repository at this point in the history
* move remaining powerpoints inside the repo

* migrate export_as_pdf

* remove grid from imports

* Update news file

* update formatters version
  • Loading branch information
ayogasekaram authored Dec 5, 2023
1 parent b53574a commit 278f0bb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 312 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 1 addition & 14 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,"%>%")
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
167 changes: 6 additions & 161 deletions R/tt_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -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() %>%
Expand All @@ -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) {
Expand All @@ -635,7 +481,6 @@ export_as_pdf <- function(tt,
.tab_to_colpath_set(fulltab)
)
}

# Flextable and docx -----------------------------------------------------------
#' Export as word document
#'
Expand Down
134 changes: 0 additions & 134 deletions man/export_as_pdf.Rd

This file was deleted.

Loading

0 comments on commit 278f0bb

Please sign in to comment.