Skip to content

Commit

Permalink
Merge branch 'main' into fix_content_row_end
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkliming committed Nov 27, 2023
2 parents c4fd5fd + 21f1aae commit b4a85d3
Show file tree
Hide file tree
Showing 23 changed files with 319 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# R specific hooks: https://github.com/lorenzwalthert/precommit
repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9023
rev: v0.3.2.9027
hooks:
# - id: style-files
# args: [--style_pkg=styler, --style_fun=tidyverse_style]
Expand Down
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: formatters
Title: ASCII Formatting for Values and Tables
Version: 0.5.4.9002
Date: 2023-10-19
Version: 0.5.4.9005
Date: 2023-11-27
Authors@R: c(
person("Gabriel", "Becker", , "[email protected]", role = "aut",
comment = "original creator of the package"),
Expand Down Expand Up @@ -51,6 +51,7 @@ Collate:
'generics.R'
'labels.R'
'mpf_exporters.R'
'package.R'
'page_size.R'
'pagination.R'
'tostring.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export(propose_column_widths)
export(prov_footer)
export(ref_df_row)
export(round_fmt)
export(set_default_hsep)
export(spans_to_viscell)
export(spread_integer)
export(sprintf_format)
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## formatters 0.5.4.9002
## formatters 0.5.4.9005
* Applied `styler` and resolved package lint. Changed default indentation from 4 spaces to 2.
* Allow tables with content rows in the end be exported.
* Fixed wrapping and section dividers error.
* Allowed section divider between header and table body.
* Added the possibility of setting a general default using `set_default_hsep()` that sets up the option `getOption("formatters_default_hsep")`.

## 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
2 changes: 0 additions & 2 deletions R/format_value.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#' @importFrom htmltools tags tagList

formats_1d <- c(
"xx", "xx.", "xx.x", "xx.xx", "xx.xxx", "xx.xxxx",
"xx%", "xx.%", "xx.x%", "xx.xx%", "xx.xxx%", "(N=xx)", ">999.9", ">999.99",
Expand Down
37 changes: 37 additions & 0 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,40 @@ setGeneric("num_rep_cols", function(obj) standardGeneric("num_rep_cols"))
#' @export
#' @rdname num_rep_cols
setMethod("num_rep_cols", "ANY", function(obj) 0L)

# header_section_div -----------------------------------------------------------
#' @keywords internal
setGeneric("header_section_div", function(obj) standardGeneric("header_section_div"))
#' @keywords internal
setMethod(
"header_section_div", "MatrixPrintForm",
function(obj) obj$header_section_div
)
#' @keywords internal
setGeneric("header_section_div<-", function(obj, value) standardGeneric("header_section_div<-"))
#' @keywords internal
setMethod(
"header_section_div<-", "MatrixPrintForm",
function(obj, value) {
obj$header_section_div <- value
obj
}
)
# horizontal_sep ---------------------------------------------------------------
#' @keywords internal
setGeneric("horizontal_sep", function(obj) standardGeneric("horizontal_sep"))
#' @keywords internal
setMethod(
"horizontal_sep", "MatrixPrintForm",
function(obj) obj$horizontal_sep
)
#' @keywords internal
setGeneric("horizontal_sep<-", function(obj, value) standardGeneric("horizontal_sep<-"))
#' @keywords internal
setMethod(
"horizontal_sep<-", "MatrixPrintForm",
function(obj, value) {
obj$horizontal_sep <- value
obj
}
)
35 changes: 16 additions & 19 deletions R/matrix_form.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,12 @@ disp_from_spans <- function(spans) {
#' @param page_titles character. Page-specific titles, as a character
#' vector.
#' @param main_footer character(1). Main footer as a string.
#' @param prov_footer character. Provenance footer information as a
#' @param prov_footer character. Provenance footer information as a
#' character vector.
#' @param header_section_div character(1). Divider to be used between header
#' and body sections.
#' @param horizontal_sep character(1). Horizontal separator to be used for printing
#' divisors between header and table body and between different footers.
#' @param expand_newlines logical(1). Should the matrix form generated
#' expand rows whose values contain newlines into multiple
#' 'physical' rows (as they will appear when rendered into
Expand Down Expand Up @@ -224,6 +228,8 @@ disp_from_spans <- function(spans) {
#' \item{\code{page_titles}}{see argument}
#' \item{\code{main_footer}}{see argument}
#' \item{\code{prov_footer}}{see argument}
#' \item{\code{header_section_div}}{see argument}
#' \item{\code{horizontal_sep}}{see argument}
#' \item{\code{col_gap}}{see argument}
#' \item{\code{table_inset}}{see argument}
#' }
Expand Down Expand Up @@ -252,6 +258,8 @@ MatrixPrintForm <- function(strings = NULL,
page_titles = character(),
main_footer = "",
prov_footer = character(),
header_section_div = NA_character_,
horizontal_sep = default_hsep(),
col_gap = 3,
table_inset = 0L,
colwidths = NULL,
Expand All @@ -274,6 +282,8 @@ MatrixPrintForm <- function(strings = NULL,
page_titles = page_titles,
main_footer = main_footer,
prov_footer = prov_footer,
header_section_div = header_section_div,
horizontal_sep = horizontal_sep,
col_gap = col_gap,
table_inset = as.integer(table_inset),
has_topleft = has_topleft,
Expand Down Expand Up @@ -399,7 +409,7 @@ infer_ref_info <- function(mform, colspace_only) {
ret$col_path <- replicate(nrow(ret), list(NA_character_))
non_na_col <- !is.na(ret$col)
ret$col_path[non_na_col] <- col_pths[ret$col[non_na_col]]
ret$ref_index <- seq_len(nrow(ret))
ret$ref_index <- match(ret$symbol, unique(ret$symbol))
##
ret$nlines <- vapply(paste0("{", ret$symbol, "} - ", ret$msg), nlines, 1L)
ret <- ret[, names(ref_df_row())]
Expand Down Expand Up @@ -891,20 +901,6 @@ reconstruct_basic_fnote_list <- function(mf) {
paste0("{", refdf$symbol, "} - ", refdf$msg)
}


fix_fnote_df <- function(df) {
ind_symb <- df$symbol == as.character(df$ref_index)
df$ref_index <- seq_len(nrow(df))
df$symbol[ind_symb] <- as.character(df$ref_index[ind_symb])
df
}







.mf_subset_core_mats <- function(mf, i, row = TRUE) {
fillnum <- if (row) nrow(mf_strings(mf)) - mf_nlheader(mf) else ncol(mf)
if (is.logical(i) || all(i < 0)) {
Expand Down Expand Up @@ -975,8 +971,10 @@ mpf_subset_rows <- function(mf, i) {

ncs <- ncol(mf)
mf <- .mf_subset_core_mats(mf, i, row = TRUE)
map <- data.frame(old_idx = c(seq_len(ncolrows), i + ncolrows),
new_idx = c(seq_len(ncolrows), ncolrows + order(i)))
map <- data.frame(
old_idx = c(seq_len(ncolrows), i + ncolrows),
new_idx = c(seq_len(ncolrows), ncolrows + order(i))
)

row_map <- data.frame(old_idx = i, new_idx = order(i))

Expand All @@ -985,7 +983,6 @@ mpf_subset_rows <- function(mf, i) {
old_nas <- is.na(refdf$row)
refdf$row <- map_to_new(refdf$row, row_map)
refdf <- refdf[old_nas | !is.na(refdf$row), ]
refdf <- fix_fnote_df(refdf)
mf_fnote_df(mf) <- refdf

rinfo <- mf_rinfo(mf)
Expand Down
4 changes: 3 additions & 1 deletion R/mpf_exporters.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export_as_txt <- function(x,
cpp = NA_integer_,
lpp = NA_integer_,
...,
hsep = default_hsep(),
hsep = NULL,
indent_size = 2,
tf_wrap = paginate,
max_width = NULL,
Expand All @@ -57,6 +57,8 @@ 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
11 changes: 11 additions & 0 deletions R/package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' formatters Package
#'
#' Package to format tables and listings in a flexible way.
#'
"_PACKAGE"

#' @importFrom stats na.omit
#' @importFrom utils head tail localeToCharset
#' @importFrom htmltools tags tagList
#' @import checkmate
NULL
Loading

0 comments on commit b4a85d3

Please sign in to comment.