Skip to content

Commit

Permalink
94 internalize function@main (#234)
Browse files Browse the repository at this point in the history
closes #94

---------

Signed-off-by: Abinaya Yogasekaram <[email protected]>
Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Emily de la Rua <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent afcaaac commit 576bcbd
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 5 deletions.
17 changes: 16 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export(default_hsep)
export(diagnose_pagination)
export(divider_height)
export(do_forced_paginate)
export(export_as_pdf)
export(export_as_rtf)
export(export_as_txt)
export(fmt_config)
export(font_lcpi)
export(format_value)
export(is.wholenumber)
export(is_valid_format)
Expand Down Expand Up @@ -134,9 +134,24 @@ import(checkmate)
import(grDevices)
import(grid)
import(methods)
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(stats,na.omit)
importFrom(tools,file_ext)
importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,localeToCharset)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added the possibility of setting a general default using `set_default_hsep()` that sets up the option `getOption("formatters_default_hsep")`.
* Fixed infinite loop in `wrap_string` that was caused by a bug in `stringi::stri_wrap` not wrapping small
strings with dots and spaces correctly.
* Migrated `export_as_pdf` from `rtables`. Now using `paginate_to_mpfs` function. Made `font_lcpi` function internal.

## formatters 0.5.4
* Fixed a bug in `paginate_to_mpfs()` so that formatting in listings key columns is retained with pagination [`insightsengineering/rlistings#155`](https://github.com/insightsengineering/rlistings/issues/155).
Expand Down
175 changes: 173 additions & 2 deletions R/mpf_exporters.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export_as_txt <- function(x,
rep_cols = num_rep_cols(x),
verbose = FALSE,
page_break = "\\s\\n") {


if (paginate) {
pages <- paginate_to_mpfs(
x,
Expand Down Expand Up @@ -463,3 +461,176 @@ export_as_rtf <- function(x,
restxt
}
}


#' Export as PDF
#'
#' The PDF output is based on the ASCII output created with [toString()]
#'
#' @inheritParams export_as_txt
#' @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.
#'
#' @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 [export_as_txt()]
#'
#' @importFrom grid textGrob get.gpar
#' @importFrom grDevices dev.off
#' @importFrom tools file_ext
#' @export
#'
#' @examples
#' \dontrun{
#' tf <- tempfile(fileext = ".pdf")
#' export_as_pdf(basic_matrix_form(mtcars), file = tf, pg_height = 4)
#'
#' tf <- tempfile(fileext = ".pdf")
#' export_as_pdf(basic_matrix_form(mtcars), file = tf, lpp = 8)
#' }
export_as_pdf <- function(x,
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),
min_siblings = 2,
font_family = "Courier",
font_size = 8,
fontsize = font_size,
paginate = TRUE,
lpp = NULL,
cpp = NULL,
hsep = "-",
indent_size = 2,
tf_wrap = TRUE,
max_width = NULL,
colwidths = propose_column_widths(x)) {
stopifnot(tools::file_ext(file) != ".pdf")
if (!is.null(colwidths) && length(colwidths) != ncol(x) + 1) {
stop(
"non-null colwidths argument must have length ncol(x) + 1 [",
ncol(x) + 1, "], got length ", length(colwidths)
)
}
gp_plot <- grid::gpar(fontsize = font_size, fontfamily = font_family)

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::grid.newpage()
grid::pushViewport(grid::plotViewport(margins = margins, gp = gp_plot))

cur_gpar <- grid::get.gpar()
if (is.null(lpp)) {
lpp <- floor(grid::convertHeight(grid::unit(1, "npc"), "lines", valueOnly = TRUE) /
(cur_gpar$cex * cur_gpar$lineheight)) - sum(margins[c(1, 3)]) # bottom, top # nolint
}
if (is.null(cpp)) {
cpp <- floor(grid::convertWidth(grid::unit(1, "npc"), "inches", valueOnly = TRUE) *
font_lcpi(font_family, font_size, cur_gpar$lineheight)$cpi) - sum(margins[c(2, 4)]) # left, right # nolint
}
if (tf_wrap && is.null(max_width)) {
max_width <- cpp
}

if (paginate) {
tbls <- paginate_to_mpfs(
x,
page_type = page_type,
font_family = font_family,
font_size = font_size,
lineheight = cur_gpar$lineheight,
landscape = landscape,
pg_width = pg_width,
pg_height = pg_height,
margins = margins,
lpp = lpp,
cpp = cpp,
min_siblings = min_siblings,
nosplitin = character(),
colwidths = colwidths,
tf_wrap = tf_wrap,
max_width = max_width,
indent_size = indent_size,
verbose = FALSE,
rep_cols = num_rep_cols(x)
)
} else {
mf <- matrix_form(x, TRUE, TRUE, indent_size = indent_size)
mf_col_widths(mf) <- colwidths %||% propose_column_widths(mf)
tbls <- list(mf)
}

gtbls <- lapply(tbls, function(txt) {
grid::textGrob(
label = toString(txt,
widths = txt$col_widths + 1, hsep = hsep,
tf_wrap = tf_wrap, max_width = max_width
),
x = grid::unit(0, "npc"), y = grid::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::grid.newpage()
grid::pushViewport(grid::plotViewport(margins = margins, gp = gp_plot))
}

if (grid::convertHeight(grid::grobHeight(g), "inches", valueOnly = TRUE) >
grid::convertHeight(grid::unit(1, "npc"), "inches", valueOnly = TRUE)) { # nolint
exceeds_height[i] <- TRUE
warning("height of page ", i, " exceeds the available space")
}
if (grid::convertWidth(grid::grobWidth(g), "inches", valueOnly = TRUE) >
grid::convertWidth(grid::unit(1, "npc"), "inches", valueOnly = TRUE)) { # nolint
exceeds_width[i] <- TRUE
warning("width of page ", i, " exceeds the available space")
}

grid::grid.draw(g)
}
list(
file = file, npages = npages, exceeds_width = exceeds_width, exceeds_height = exceeds_height,
lpp = lpp, cpp = cpp
)
}
5 changes: 4 additions & 1 deletion R/page_size.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ page_dim <- function(page_type) {
#' @return named list with `cpi` and `lpi`, the characters and lines per
#' inch, respectively.
#'
#' @export
#' @examples
#' \dontrun{
#' font_lcpi()
#'
#' font_lcpi(font_size = 8)
#'
#' font_lcpi(font_size = 8, lineheight = 1.1)
#' }
#'
#' @keywords internal
font_lcpi <- function(font_family = "Courier", font_size = 8, lineheight = 1) {
tmppdf <- tempfile(fileext = ".pdf")
pdf(tmppdf)
Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ reference:
contents:
- export_as_txt
- export_as_rtf
- export_as_pdf
- mpf_to_rtf
- title: Miscellany
desc: Other documented functions not interesting to end users
Expand All @@ -102,7 +103,6 @@ reference:
- ifnotlen0
- table_inset
- default_horizontal_sep
- font_lcpi
- mf_strings
- page_lcpp
- page_types
Expand Down
128 changes: 128 additions & 0 deletions man/export_as_pdf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 576bcbd

Please sign in to comment.