Skip to content

Commit

Permalink
adding last things
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Sep 20, 2023
1 parent 1cc86c0 commit 76b18f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
36 changes: 28 additions & 8 deletions R/tt_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ margins_landscape <- function() {
#' @param border `officer` border object. Defaults to `officer::fp_border(width = 0.5)`.
#' @param indent_size integer(1). If `NULL`, the default indent size of the table (see
#' [matrix_form()] `indent_size`) is used. To work with `docx`, any size is multiplied
#' by 5 as default.
#' by 2 mm (5.67 pt) as default.
#' @param titles_as_header logical(1). Defaults to `TRUE` and makes additional header rows
#' for [main_title()] string and [subtitles()] character vector (one per element). If `FALSE`
#' it is still possible to use the same parameter in [export_as_docx()] to add these titles
Expand Down Expand Up @@ -649,7 +649,8 @@ tt_to_flextable <- function(tt,
))
}
# Calculate the needed colwidths
final_cwidths <- total_width * colwidths / sum(colwidths)
final_cwidths <- total_width * colwidths / sum(colwidths) # xxx to fix
# xxx FIXME missing transformer from character based widths to mm or pt

# Extract relevant information
matform <- matrix_form(tt, indent_rownames = TRUE)
Expand Down Expand Up @@ -691,7 +692,7 @@ tt_to_flextable <- function(tt,
flx,
top = TRUE,
values = as.vector(hdr[i, sel]),
colwidths = as.integer(matform$spans[i, sel])
colwidths = as.integer(matform$spans[i, sel]) # xxx to fix
)
}
}
Expand All @@ -709,12 +710,13 @@ tt_to_flextable <- function(tt,
# Rownames indentation
checkmate::check_int(indent_size, null.ok = TRUE)
if (is.null(indent_size)) {
indent_size <- matform$indent_size * 5
indent_size <- matform$indent_size * word_mm_to_pt(2) # default is 2mm (5.7pt)
}
for (i in seq_len(NROW(tt))) {
flx <- flextable::padding(flx,
i = i, j = 1,
padding.left = indent_size * rdf$indent[[i]]
padding.left = indent_size * rdf$indent[[i]] + word_mm_to_pt(0.1), # 0.1 mmm in pt
padding.right = word_mm_to_pt(0.1) # 0.1 mmm in pt (so not to touch the border)
)
}

Expand All @@ -729,7 +731,7 @@ tt_to_flextable <- function(tt,
flx <- flextable::add_footer_lines(flx, values = all_footers(tt))
}

flx <- flextable::width(flx, width = final_cwidths)
flx <- flextable::width(flx, width = final_cwidths) # xxx to fix

if (!is.null(theme)) {
flx <- theme(flx)
Expand All @@ -752,8 +754,8 @@ tt_to_flextable <- function(tt,
remove_vborder(part = "header", ii = seq_along(real_titles))
}
# These final formatting need to work with colwidths
flx <- flextable::set_table_properties(flx, layout = "autofit")
flx <- flextable::fix_border_issues(flx) # needed?`
flx <- flextable::set_table_properties(flx, layout = "autofit") # xxx to fix
flx <- flextable::fix_border_issues(flx) # needed? # xxx to fix
flx
}

Expand Down Expand Up @@ -868,9 +870,27 @@ theme_docx_default <- function(tt = NULL, # Option for more complicated stuff
)
}
}

# vertical padding is manual atm and respect doc std
flx <- flx %>%
# flextable::padding(j = 2:(NCOL(tt) + 1), padding.top = , part = "body") %>% # not specified
flextable::padding(j = 1, padding.top = 1, padding.bottom = 1, part = "body") %>%
flextable::padding(j = 2:(NCOL(tt) + 1), padding.top = 0, padding.bottom = 3, part = "header")

# single line spacing (for safety) -> space = 1
flx <- flextable::line_spacing(flx, space = 1, part = "all")

flx
}
}
# Padding helper functions to transform mm to pt and viceversa
# # General note for word: 1pt -> 0.3527777778mm -> 0.013888888888889"
word_inch_to_pt <- function(inch) { # nocov
inch / 0.013888888888889 # nocov
}
word_mm_to_pt <- function(mm) {
mm / 0.3527777778
}

# Polish horizontal borders
remove_hborder <- function(flx, part, w = c("top", "bottom", "inner")) {
Expand Down
2 changes: 1 addition & 1 deletion man/tt_to_flextable.Rd

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

0 comments on commit 76b18f0

Please sign in to comment.