Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tidytlg 0.11.0.9000

- Updated `gentlg_single()` to replace, in the first column of tables/listings headers, every leading whitespace with 90 twips (0.0625 inches) left-indentation RTF markup.

# tidytlg 0.11.0

- Fixed RTF landscape tag (#54).
Expand Down
6 changes: 5 additions & 1 deletion R/gentlg_single.R
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this needs tests as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added unit tests for replace_lead_whitespaces_ind()

Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
#############################
### Huxit! ###
#############################

if (is_format_rtf(format)) {
if (tolower(substr(tlf, 1, 1)) %in% c("t")) {
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) |>
Expand All @@ -487,12 +488,14 @@
"Column header not used; {length(colheader)} column header provided, but data contain {ncol(ht)} columns"
)
}
ht[1, 1] <- replace_lead_whitespaces_ind(ht[1, 1])
ht[1, ] <- paste0("\\keepn\\trhdr ", ht[1, ]) # Make repeated treatments on each page
formatindex <- 1
} else if (tolower(substr(tlf, 1, 1)) %in% c("l")) {
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) |>
huxtable::set_width(value = huxwidth)
ht[1, ] <- colheader
ht[1, 1] <- replace_lead_whitespaces_ind(ht[1, 1])
ht[1, ] <- paste0("\\keepn\\trhdr ", ht[1, ]) # Make repeated treatments on each page
formatindex <- 1
} else if (tolower(substr(tlf, 1, 1)) %in% c("f", "g")) {
Expand Down Expand Up @@ -562,7 +565,8 @@

### add row one by one to maintain huxtable structure
if (is_format_rtf(format)) {
for (i in rev(seq_along(colspan))) {
for (i in rev(seq_len(length(colspan)))) {

Check warning on line 568 in R/gentlg_single.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/gentlg_single.R,line=568,col=21,[seq_linter] Use seq_along(x) instead of seq_len(length(x)).
colspan[[i]][1] <- replace_lead_whitespaces_ind(colspan[[i]][1])
ht <- huxtable::insert_row(ht, paste0("\\keepn\\trhdr ", colspan[[i]]),
after = 0, fill = ""
) |>
Expand Down
29 changes: 29 additions & 0 deletions R/replace_lead_whitespaces_ind.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' Replace leading whitespaces with left indentation RTF markup
#'
#' @details
#' The following function receives a string 'x' and returns a modified string
#' where every leading whitespace is replaced with 90 twips (0.0625 inches) left indentation RTF markup.
#' If the input does not start with a whitespace, the string is returned as is.
#'
#' @param x `character(1)` a string to replace the leading whitespaces.
#'
#' @examples
#' replace_lead_whitespaces_ind(" this is x")
#' # [1] "\\intbl\\li360\\fi0 this is x"
#' replace_lead_whitespaces_ind("this is x")
#' # [1] "this is x"
#'
#' @return `character(1)` RTF markup with leading whitespaces replaced.
#' @keywords internal
replace_lead_whitespaces_ind <- function(x) {
# get number of leading whitespaces
num_whitespaces <- attr(regexpr("^\\s*", x), "match.length")
# 2 whitespaces represent an indentation of 0.125 inches = 180 twips
# e.g. 4 whitespaces represent a left-indentation of 0.25 inches = 360 twips
num_twips <- num_whitespaces * 90
if (num_twips > 0) {
raw_rtf_markup <- paste0("\\intbl\\li", num_twips, "\\fi0")
x <- paste0(raw_rtf_markup, " ", trimws(x, "left"))
}
x
}
30 changes: 30 additions & 0 deletions man/replace_lead_whitespaces_ind.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-gentlg.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ test_that("custom alignments work", {
expect_equal(align_property_2[4, 2], "center")
expect_equal(align_property_2[3, 3], "right")
})

test_that("replace_lead_whitespaces_ind() is replacing whitespaces", {
# case when there are 2 leading whitespaces, should insert 180 twips
df <- data.frame(label = c("boy", "girl"), name = c("Bob", "Lily"), age = c(12, 15))
expect_no_error(res <- gentlg(huxme = df,
print.hux = FALSE,
colheader = c(" Gender", "Name", "Age")))
res <- as.character(res[[1]][2, 1])
expect_equal(res, "\\keepn\\trhdr \\intbl\\li180\\fi0 Gender")

# case when there are no leading whitespaces, should leave it as is
expect_no_error(res <- gentlg(huxme = df,
print.hux = FALSE,
colheader = c("Gender", "Name", "Age")))
res <- as.character(res[[1]][2, 1])
expect_equal(res, "\\keepn\\trhdr Gender")
})
Loading