Skip to content
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

Merged
merged 25 commits into from
Oct 18, 2023

Conversation

Melkiades
Copy link
Contributor

(pagination does not count it though for now) needed for insightsengineering/rtables#750

@Melkiades Melkiades added bug Something isn't working sme Tracks changes for the sme board labels Oct 17, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 17, 2023

badge

Code Coverage Summary

Filename             Stmts    Miss  Cover    Missing
-----------------  -------  ------  -------  ---------------------------------------------------------------------------------------------------------------
R/format_value.R       197      14  92.89%   94, 110-117, 197, 216, 287, 409, 420, 428
R/generics.R            98       4  95.92%   455, 467, 500, 529
R/labels.R              55       7  87.27%   53, 59, 68, 109, 137, 146, 150
R/matrix_form.R        454      27  94.05%   400-401, 496, 508-509, 528, 559, 648-649, 664-669, 696-697, 729-730, 762-763, 795, 881, 929, 981, 983, 986
R/mpf_exporters.R      124      12  90.32%   2, 84-86, 180, 220, 225, 408, 410, 415-416, 442
R/page_size.R           45       1  97.78%   169
R/pagination.R         503      27  94.63%   421, 493, 613-614, 619, 673-674, 692-700, 986, 993, 1018-1025, 1160-1161, 1173-1174, 1186-1187
R/tostring.R           521      26  95.01%   83, 135, 202, 236, 244-245, 280, 333-334, 423-425, 432-435, 610-611, 803, 818, 913, 966, 1007, 1052, 1107, 1114
R/utils.R                3       0  100.00%
TOTAL                 2000     118  94.10%

Diff against main

Filename           Stmts    Miss  Cover
---------------  -------  ------  -------
R/matrix_form.R      +27       0  +0.38%
R/pagination.R        +5      +5  -0.95%
R/tostring.R         +22      +1  +0.02%
TOTAL                +54      +6  -0.14%

Results for commit: 8b5c380

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

@github-actions
Copy link
Contributor

github-actions bot commented Oct 17, 2023

Unit Tests Summary

    1 files      4 suites   8s ⏱️
  33 tests   33 ✔️ 0 💤 0
228 runs  228 ✔️ 0 💤 0

Results for commit 8b5c380.

♻️ This comment has been updated with latest results.

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
Copy link
Contributor Author

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

Comment on lines +764 to +765
if (!setequal(ori_wrapped_txt_v, cur_wrapped_txt_v)) {
return(wrap_string(str = ret_collapse, width = width, collapse = collapse))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids infinite loop

Comment on lines +35 to +41
# 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)
Copy link
Contributor Author

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

Comment on lines +27 to +28
tl <- strmat[nl_inds_header, 1, drop = TRUE]
strmat[nl_inds_header, 1] <- ""
Copy link
Contributor Author

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)

Comment on lines +105 to +112
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]]
Copy link
Contributor Author

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)

Comment on lines +561 to +565
# 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))
}

Copy link
Contributor Author

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
Copy link
Contributor Author

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compacted it

Comment on lines +768 to +770
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)
Copy link
Contributor Author

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)])
Copy link
Contributor Author

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)

Comment on lines +107 to 109
expct_lns <- c(" lo ",
" hi there ",
"(N=50) (N=whoknows)",
Copy link
Contributor Author

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

Comment on lines +101 to 102
has_topleft = FALSE,
line_grouping = c(1, 1, 2, 3, 4),
Copy link
Contributor Author

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),
Copy link
Contributor Author

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

@shajoezhu shajoezhu enabled auto-merge (squash) October 18, 2023 03:42
Copy link
Contributor

@shajoezhu shajoezhu left a 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

@shajoezhu shajoezhu merged commit 42dceb4 into main Oct 18, 2023
14 of 18 checks passed
@shajoezhu shajoezhu deleted the 750_solve_nl_in_export_as_txt@main branch October 18, 2023 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sme Tracks changes for the sme board
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants