-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add newline character support for ref footnotes #215
Conversation
Code Coverage Summary
Diff against main
Results for commit: 8b5c380 Minimum allowed coverage is ♻️ This comment has been updated with latest results |
…htsengineering/formatters into 750_solve_nl_in_export_as_txt@main
page_indices$pag_row_indices, | ||
function(ii) { | ||
mf_tmp <- matrix_form(obj[ii, ], TRUE, TRUE, indent_size = indent_size) | ||
mf_col_widths(mf_tmp) <- colwidths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shajoezhu very important fix that was avoiding the wrapping when paginated
if (!setequal(ori_wrapped_txt_v, cur_wrapped_txt_v)) { | ||
return(wrap_string(str = ret_collapse, width = width, collapse = collapse)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This avoids infinite loop
# pre-proc in case of wrapping and \n | ||
line_grouping <- mf_lgrouping(matform) | ||
strmat <- .compress_mat(strmat, line_grouping, "nl") | ||
frmmat <- .compress_mat(frmmat, line_grouping, "unique") # never not unique | ||
spamat <- .compress_mat(spamat, line_grouping, "unique") | ||
alimat <- .compress_mat(alimat, line_grouping, "unique") | ||
line_grouping <- unique(line_grouping) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this compression is needed when columns are wrapped. Without it we get a weird bottom alignment if there are \n preceding this
tl <- strmat[nl_inds_header, 1, drop = TRUE] | ||
strmat[nl_inds_header, 1] <- "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix is needed when the number of rows are not the number of lines of the header (we need to get topleft out and add it back later to respect the right line grouping)
val <- unique(col_vec) | ||
val <- val[nzchar(val)] | ||
if (length(val) > 1) { | ||
stop("Problem in linegroupings! Some do not have the same values.") # nocov | ||
} else if(length(val) < 1) { | ||
val <- "" # Case in which it is only "" | ||
} | ||
val[[1]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are done for formats (that can be not unique, e.g. c("1", "")
and val[[1]]
is needed when there are functions (automatic cast to lists)
# Fix for ref_fnotes with \n characters XXX this does not count in the pagination | ||
if (any(grepl("\n", ref_fnotes))) { | ||
ref_fnotes <- unlist(strsplit(ref_fnotes, "\n", fixed = TRUE)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a safenet, being it in toString, it will not update the number of lines for pagination but neither break (I do not expect this to be used at all in general)
@@ -678,11 +684,6 @@ new_line_warning <- function(str_v) { | |||
#' @param collapse character(1) or `NULL`. If the words that have been split should | |||
#' be pasted together with the collapse character. This is usually done internally | |||
#' with `"\n"` to have the wrapping updated along with other internal values. | |||
#' @param smart logical(1). Defaults to `TRUE`. It attempts to calculate the optimal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use only the "smart" recursive algorithm
@@ -778,14 +779,40 @@ wrap_string <- function(str, width, collapse = NULL, smart = TRUE) { | |||
return(ret) | |||
} | |||
|
|||
.go_stri_wrap <- function(str, w) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compacted it
ret_tmp <- force_split_words_by(ret[we_interval], width) # here we_interval is only one ind | ||
ret <- append(ret, ret_tmp, we_interval)[-we_interval] | ||
which_exceeded <- which(nchar(ret) > width) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
force_split was necessary because stringi
appears unable to split certain characters such as "x ." into smaller elements. I suspect that space and full stop are considered one word (even if normalize = TRUE) so it gets stuck in an eternal recursion, so we need to break it manually, append it and redefine the index of what is needed to be modified
init_v <- seq(1, nchar(wrd_i), by = width) | ||
end_v <- c(init_v[-1] - 1, nchar(wrd_i)) | ||
str_v_tmp <- stringi::stri_sub(wrd_i, from = init_v, to = end_v) | ||
ret_tmp <- c(ret_tmp, str_v_tmp[!grepl("^\\s+$", str_v_tmp) & nzchar(str_v_tmp)]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoiding empties is needed at this stage to avoid spurious spaces appearing (it should not happen too often to get here anyway, only with seriously small widths)
expct_lns <- c(" lo ", | ||
" hi there ", | ||
"(N=50) (N=whoknows)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I value this as an improvement
has_topleft = FALSE, | ||
line_grouping = c(1, 1, 2, 3, 4), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not having to add topleft info was a problem as it was mistakingly considering the first colname as a topleft info
@@ -135,7 +135,7 @@ matrix_form.data.frame <- function(df) { | |||
strings = strings, | |||
aligns = aligns, | |||
spans = matrix(1, nrow = fnr, ncol = fnc), | |||
formats = NULL, | |||
formats = matrix("", nrow = fnr, ncol = fnc), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be allowed in general
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot @Melkiades , i am merging this in to trigger UAT
(pagination does not count it though for now) needed for insightsengineering/rtables#750