Skip to content

Commit

Permalink
refining split
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Nov 20, 2024
1 parent bd9545d commit a1bb3ab
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 337 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Collate:
'package.R'
'tt_export.R'
'tt_as_flextable.R'
'tt_to_paginate_office.R'
'export_as_docx.R'
'as_flextable.R'
8 changes: 0 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(export_as_docx)
export(export_as_pdf)
export(export_as_txt)
export(margins_landscape)
export(margins_potrait)
export(section_properties_default)
Expand All @@ -11,14 +9,8 @@ export(theme_html_default)
export(tt_to_flextable)
export(word_mm_to_pt)
import(flextable)
import(formatters)
import(methods)
import(officer)
import(rtables)
importFrom(formatters,export_as_pdf)
importFrom(formatters,export_as_txt)
importFrom(lifecycle,deprecated)
importFrom(magrittr,"%>%")
importFrom(rtables,import_from_tsv)
importFrom(stats,setNames)
importFrom(tools,file_ext)
35 changes: 23 additions & 12 deletions R/tt_as_flextable.R → R/as_flextable.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# Helper function for position to path
pos_to_path <- function(pos) {
spls <- rtables:::pos_splits(pos)
vals <- rtables:::pos_splvals(pos)

path <- character()
for (i in seq_along(spls)) {
nm <- obj_name(spls[[i]])
val_i <- value_names(vals[[i]])
path <- c(
path,
obj_name(spls[[i]]),
## rawvalues(vals[[i]]))
if (!is.na(val_i)) val_i
)
}
path
}

# Flextable conversion ---------------------------------------------------------
#

Expand Down Expand Up @@ -82,17 +101,14 @@
#'
#' tbl <- build_table(lyt, ex_adsl)
#'
#' @examples
#' # example code
#'
#' # rtables style
#' tt_to_flextable(tbl, theme = NULL)
#'
#' tt_to_flextable(tbl, theme = theme_docx_default(font_size = 6))
#'
#' # Example with multiple themes (only extending the docx default!)
#' my_theme <- function(x, ...) {
#' flextable::border_inner(x, part = "body", border = flextable::fp_border_default(width = 0.5))
#' border_inner(x, part = "body", border = flextable::fp_border_default(width = 0.5))
#' }
#' flx <- tt_to_flextable(tbl, theme = c(theme_docx_default(), my_theme))
#'
Expand All @@ -116,7 +132,6 @@ tt_to_flextable <- function(tt,
total_page_height = 10, # portrait 11 landscape 8.5
total_page_width = 10, # portrait 8.5 landscape 11
autofit_to_page = TRUE) {
check_required_packages("flextable")
if (!inherits(tt, "VTableTree")) {
stop("Input table is not an rtables' object.")
}
Expand Down Expand Up @@ -521,7 +536,8 @@ tt_to_flextable <- function(tt,
#'
#' @seealso [export_as_docx()]
#'
#' @examples
#' @examplesIf require(flextable)
#' library(flextable)
#' # Custom theme
#' special_bold <- list(
#' "header" = list("i" = 1, "j" = c(1, 3)),
Expand All @@ -546,10 +562,7 @@ tt_to_flextable <- function(tt,
#' flx <- theme_docx_default(font_size = font_size)(flx, ...)
#'
#' # Then apply additional styling
#' flx <- flextable::border_inner(flx,
#' part = "body",
#' border = flextable::fp_border_default(width = 0.5)
#' )
#' flx <- border_inner(flx, part = "body", border = flextable::fp_border_default(width = 0.5))
#'
#' return(flx)
#' }
Expand All @@ -569,7 +582,6 @@ theme_docx_default <- function(font = "Arial",
bold_manual = NULL,
border = flextable::fp_border_default(width = 0.5)) {
function(flx, ...) {
check_required_packages("flextable")
if (!inherits(flx, "flextable")) {
stop(sprintf(
"Function `%s` supports only flextable objects.",
Expand Down Expand Up @@ -670,7 +682,6 @@ theme_html_default <- function(font = "Courier",
remove_internal_borders = "label_rows",
border = flextable::fp_border_default(width = 1, color = "black")) {
function(flx, ...) {
check_required_packages("flextable")
if (!inherits(flx, "flextable")) {
stop(sprintf(
"Function `%s` supports only flextable objects.",
Expand Down
108 changes: 47 additions & 61 deletions R/tt_export.R → R/export_as_docx.R
Original file line number Diff line number Diff line change
@@ -1,59 +1,3 @@
#' @importFrom tools file_ext
NULL

check_required_packages <- function(pkgs) {
for (pkgi in pkgs) {
if (!requireNamespace(pkgi, quietly = TRUE)) {
stop(
"This function requires the ", pkgi, " package. ",
"Please install it if you wish to use it"
)
}
}
}


# txt (formatters) --------------------------------------------------------------------
#' @importFrom formatters export_as_txt
#'
#' @examples
#' lyt <- basic_table() %>%
#' split_cols_by("ARM") %>%
#' analyze(c("AGE", "BMRKR2", "COUNTRY"))
#'
#' tbl <- build_table(lyt, ex_adsl)
#'
#' cat(export_as_txt(tbl, file = NULL, paginate = TRUE, lpp = 8))
#'
#' \dontrun{
#' tf <- tempfile(fileext = ".txt")
#' export_as_txt(tbl, file = tf)
#' system2("cat", tf)
#' }
#'
#' @export
formatters::export_as_txt

# pdf (formatters) ----------------------------------------------------------
#' @importFrom formatters export_as_pdf
#'
#' @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)
#' }
#'
#' @export
formatters::export_as_pdf

# docx (flextable) -----------------------------------------------------------
#' Export as word document
#'
Expand All @@ -74,7 +18,7 @@ formatters::export_as_pdf
#' @param ... (`any`)\cr additional arguments passed to [tt_to_flextable()].
#'
#' @note `export_as_docx()` has few customization options available. If you require specific formats and details,
#' we suggest that you use [tt_to_flextable()] prior to `export_as_docx`. Only the `title_as_header` and
#' we suggest that you use [tt_to_flextable()] prior to `export_as_docx`. Only the `titles_as_header` and
#' `footer_as_text` parameters must be re-specified if the table is changed first using [tt_to_flextable()].
#'
#' @seealso [tt_to_flextable()]
Expand Down Expand Up @@ -103,7 +47,6 @@ export_as_docx <- function(tt,
section_properties = section_properties_default(),
...) {
# Checks
check_required_packages(c("flextable", "officer"))
if (inherits(tt, "VTableTree")) {
flex_tbl <- tt_to_flextable(tt,
titles_as_header = titles_as_header,
Expand All @@ -125,8 +68,36 @@ export_as_docx <- function(tt,
fpt <- officer::fp_text(font.family = font_fam, font.size = font_sz_body)
fpt_footer <- officer::fp_text(font.family = font_fam, font.size = font_sz_footer)
}
} else {
} else if (inherits(tt, "flextable")) {
flex_tbl <- tt
} else if (inherits(tt, "list")) {
export_as_docx(tt[[1]], # First paginated table that uses template_file
file = file,
doc_metadata = doc_metadata,
titles_as_header = titles_as_header,
footers_as_text = footers_as_text,
template_file = template_file,
section_properties = section_properties,
...
)
if (length(tt) > 1) {
out <- mapply(
export_as_docx,
tt = tt[-1], # Remaining paginated tables
MoreArgs = list(
file = file,
doc_metadata = doc_metadata,
titles_as_header = titles_as_header,
footers_as_text = footers_as_text,
template_file = file, # Uses the just-created file as template
section_properties = section_properties,
...
)
)
}
return()
} else {
stop("The table must be a VTableTree, a flextable, or a list of VTableTree or flextable objects.")
}
if (!is.null(template_file) && !file.exists(template_file)) {
template_file <- NULL
Expand All @@ -139,8 +110,21 @@ export_as_docx <- function(tt,
doc <- officer::read_docx()
}

if (!is.null(section_properties)) {
doc <- officer::body_set_default_section(doc, section_properties)
# page width and orientation settings
doc <- officer::body_set_default_section(doc, section_properties)
if (flex_tbl$properties$layout != "autofit") { # fixed layout
page_width <- section_properties$page_size$width
dflx <- dim(flex_tbl)
if (abs(sum(unname(dflx$widths)) - page_width) > 1e-2) {
warning(
"The total table width does not match the page width. The column widths",
" will be resized to fit the page. Please consider modifying the parameter",
" total_page_width in tt_to_flextable()."
)

final_cwidths <- page_width * unname(dflx$widths) / sum(unname(dflx$widths))
flex_tbl <- flextable::width(flex_tbl, width = final_cwidths)
}
}

# Extract title
Expand Down Expand Up @@ -175,6 +159,8 @@ export_as_docx <- function(tt,

# Save the Word document to a file
print(doc, target = file)

invisible(TRUE)
}

# Shorthand to add text paragraph
Expand Down
3 changes: 0 additions & 3 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#' @importFrom magrittr %>%
#' @importFrom stats setNames
#' @import methods
#' @import formatters
#' @import rtables
#' @importFrom rtables import_from_tsv
#' @import officer
#' @import flextable
NULL
17 changes: 0 additions & 17 deletions R/tt_to_paginate_office.R

This file was deleted.

4 changes: 2 additions & 2 deletions man/export_as_docx.Rd

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

46 changes: 0 additions & 46 deletions man/reexports.Rd

This file was deleted.

Loading

0 comments on commit a1bb3ab

Please sign in to comment.