Skip to content

Commit fe3ed81

Browse files
authored
Merge branch 'main' into main
2 parents c77d5a0 + 2493590 commit fe3ed81

File tree

14 files changed

+137
-43
lines changed

14 files changed

+137
-43
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ benchmark/*
1616
/doc/
1717
/Meta/
1818
\.DS_Store
19-
\.lintr
2019
huxtable-output\.rtf
2120
docs
2221
pkgdown/

.lintr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
linters: linters_with_defaults(
2+
line_length_linter = line_length_linter(120L),
3+
object_usage_linter = NULL,
4+
object_name_linter = object_name_linter(
5+
styles = c("snake_case", "symbols"),
6+
regexes = c(
7+
ANL = "^ANL_?[0-9A-Z_]*$",
8+
ADaM = "^r?AD[A-Z]{2,5}_?[0-9]*$",
9+
column_names = "^AVAL$|^USUBJID$|^PARAM$|^PARAMCD$"
10+
)
11+
),
12+
indentation_linter = NULL
13+
)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# tidytlg 0.11.0.9000
22

33
- Exported functions `get_file_name()` and `insert_empty_rows()`.
4+
- 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.
45

56
# tidytlg 0.11.0
67

R/gentlg_single.R

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ gentlg_single <- function(huxme = NULL,
4747
stop("'title_file' file must have columns: 'TABLE ID',
4848
'IDENTIFIER', and 'TEXT'")
4949
}
50-
title_df <- title_df %>%
50+
title_df <- title_df |>
5151
filter(`TABLE ID` == file)
5252

5353
if (is.null(title) && nrow(title_df) > 0) {
54-
title <- title_df %>%
55-
filter(str_detect(IDENTIFIER, regex("title", ignore_case = TRUE))) %>%
54+
title <- title_df |>
55+
filter(str_detect(IDENTIFIER, regex("title", ignore_case = TRUE))) |>
5656
extract2("TEXT")
5757
if (length(title) == 0) {
5858
title <- NULL
5959
}
6060
}
6161
if (is.null(footers) && nrow(title_df) > 0) {
62-
footers <- title_df %>%
62+
footers <- title_df |>
6363
filter(str_detect(
6464
IDENTIFIER,
6565
regex("^footnote", ignore_case = TRUE)
66-
)) %>%
66+
)) |>
6767
extract2("TEXT")
6868
if (length(footers) == 0) {
6969
footers <- NULL
@@ -75,21 +75,21 @@ gentlg_single <- function(huxme = NULL,
7575
cm <- attr(huxme, "column_metadata")
7676
cm <- cm[, colSums(is.na(cm)) < nrow(cm)]
7777
if (any(str_detect(names(cm), "span"))) {
78-
colspan <- cm %>%
79-
select(starts_with("span")) %>%
80-
as.list() %>%
78+
colspan <- cm |>
79+
select(starts_with("span")) |>
80+
as.list() |>
8181
unname()
8282
colspan <- colspan[!purrr::map_lgl(colspan, ~ all(is.na(.)))]
83-
colspan <- purrr::map(colspan, ~ c("", replace_na_with_blank(.))) %>%
83+
colspan <- purrr::map(colspan, ~ c("", replace_na_with_blank(.))) |>
8484
rev()
8585
}
8686
}
8787
if (!is.null(attr(huxme, "column_metadata")) && is.null(colheader)) {
8888
cm <- attr(huxme, "column_metadata")
8989
if ("decode" %in% names(cm)) {
90-
colheader <- cm %>%
91-
select(decode) %>%
92-
unlist() %>%
90+
colheader <- cm |>
91+
select(decode) |>
92+
unlist() |>
9393
unname()
9494
colheader <- c("", colheader)
9595
names(colheader) <- c("label", paste0("col", seq_len(nrow(cm))))
@@ -149,7 +149,7 @@ gentlg_single <- function(huxme = NULL,
149149

150150
# Check if the huxme is blank, this will error out in R < 4
151151
if (ncol(huxme) > 0) {
152-
huxme <- huxme %>%
152+
huxme <- huxme |>
153153
select(!c(any_of("func"), ends_with("_ord")))
154154
}
155155

@@ -332,24 +332,24 @@ gentlg_single <- function(huxme = NULL,
332332
#############################
333333

334334
.to_html <- function(df) {
335-
list_df <- df %>%
336-
as.list() %>%
335+
list_df <- df |>
336+
as.list() |>
337337
lapply(gsub,
338338
pattern = "(\\{\\\\super)(.*?)(\\})",
339339
replacement = "<sup>\\2</sup>"
340-
) %>%
340+
) |>
341341
lapply(gsub,
342342
pattern = "(\\{\\\\sub)(.*?)(\\})",
343343
replacement = "<sub>\\2</sub>"
344-
) %>%
345-
lapply(gsub, pattern = "\\\\n", replacement = "<br/>") %>%
344+
) |>
345+
lapply(gsub, pattern = "\\\\n", replacement = "<br/>") |>
346346
lapply(gsub, pattern = "\\\\line", replacement = "<br/>")
347347

348348
if (base::inherits(df, "data.frame")) {
349-
df <- list_df %>%
349+
df <- list_df |>
350350
data.frame(stringsAsFactors = FALSE, check.names = FALSE)
351351
} else if (base::inherits(df, "character") && !is.null(df)) {
352-
df <- list_df %>%
352+
df <- list_df |>
353353
unlist(use.names = FALSE)
354354
}
355355

@@ -429,7 +429,7 @@ gentlg_single <- function(huxme = NULL,
429429
}
430430

431431
if (!is.na(idvars[1]) && dim(huxme)[1] >= 1) {
432-
for (i in rev(seq_len(length(idvars)))) {
432+
for (i in rev(seq_along(idvars))) {
433433
less <- idvars[1:i]
434434
huxme[duplicated(huxme[, less]), less] <- ""
435435
}
@@ -476,9 +476,10 @@ gentlg_single <- function(huxme = NULL,
476476
#############################
477477
### Huxit! ###
478478
#############################
479+
479480
if (is_format_rtf(format)) {
480481
if (tolower(substr(tlf, 1, 1)) %in% c("t")) {
481-
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) %>%
482+
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) |>
482483
huxtable::set_width(value = huxwidth)
483484
if (ncol(ht) == length(colheader)) {
484485
ht[1, ] <- colheader
@@ -487,23 +488,25 @@ gentlg_single <- function(huxme = NULL,
487488
"Column header not used; {length(colheader)} column header provided, but data contain {ncol(ht)} columns"
488489
)
489490
}
491+
ht[1, 1] <- replace_lead_whitespaces_ind(ht[1, 1])
490492
ht[1, ] <- paste0("\\keepn\\trhdr ", ht[1, ]) # Make repeated treatments on each page
491493
formatindex <- 1
492494
} else if (tolower(substr(tlf, 1, 1)) %in% c("l")) {
493-
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) %>%
495+
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) |>
494496
huxtable::set_width(value = huxwidth)
495497
ht[1, ] <- colheader
498+
ht[1, 1] <- replace_lead_whitespaces_ind(ht[1, 1])
496499
ht[1, ] <- paste0("\\keepn\\trhdr ", ht[1, ]) # Make repeated treatments on each page
497500
formatindex <- 1
498501
} else if (tolower(substr(tlf, 1, 1)) %in% c("f", "g")) {
499-
ht <- huxtable::as_hux(huxme, add_colnames = FALSE) %>%
502+
ht <- huxtable::as_hux(huxme, add_colnames = FALSE) |>
500503
huxtable::set_width(value = huxwidth)
501504
} else {
502505
stop("tlf can have following character values: Table, Listing, Graph, Figure")
503506
}
504507
} else if (is_format_html(format)) {
505508
if (tolower(substr(tlf, 1, 1)) %in% c("t")) {
506-
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) %>%
509+
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) |>
507510
huxtable::set_width(value = huxwidth)
508511
if (ncol(ht) == length(colheader)) {
509512
ht[1, ] <- colheader
@@ -515,20 +518,20 @@ gentlg_single <- function(huxme = NULL,
515518

516519
formatindex <- 1
517520
} else if (tolower(substr(tlf, 1, 1)) %in% c("l")) {
518-
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) %>%
521+
ht <- huxtable::as_hux(huxme, add_colnames = TRUE) |>
519522
huxtable::set_width(value = huxwidth)
520523
ht[1, ] <- colheader
521524
formatindex <- 1
522525
} else if (tolower(substr(tlf, 1, 1)) %in% c("f", "g")) {
523-
ht <- huxtable::as_hux(huxme, add_colnames = FALSE) %>%
526+
ht <- huxtable::as_hux(huxme, add_colnames = FALSE) |>
524527
huxtable::set_width(value = huxwidth)
525528
} else {
526529
stop("tlf can have following character values: Table, Listing, Graph, Figure")
527530
}
528531
}
529532

530-
ht <- ht %>%
531-
huxtable::set_right_padding(value = getOption("tidytlg.right.padding")) %>%
533+
ht <- ht |>
534+
huxtable::set_right_padding(value = getOption("tidytlg.right.padding")) |>
532535
huxtable::set_left_padding(value = getOption("tidytlg.left.padding"))
533536

534537
#############################
@@ -562,15 +565,16 @@ gentlg_single <- function(huxme = NULL,
562565

563566
### add row one by one to maintain huxtable structure
564567
if (is_format_rtf(format)) {
565-
for (i in rev(seq_len(length(colspan)))) {
568+
for (i in rev(seq_along(colspan))) {
569+
colspan[[i]][1] <- replace_lead_whitespaces_ind(colspan[[i]][1])
566570
ht <- huxtable::insert_row(ht, paste0("\\keepn\\trhdr ", colspan[[i]]),
567571
after = 0, fill = ""
568-
) %>%
572+
) |>
569573
huxtable::set_number_format(row = 1, col = seq_len(ncol(ht)), value = NA)
570574
}
571575
} else if (is_format_html(format)) {
572576
### add row one by one to maintain huxtable structure
573-
for (i in rev(seq_len(length(colspan)))) {
577+
for (i in rev(seq_along(colspan))) {
574578
ht <- huxtable::insert_row(ht, paste0(colspan[[i]]), after = 0)
575579
}
576580
}
@@ -612,10 +616,10 @@ gentlg_single <- function(huxme = NULL,
612616

613617
### font size
614618
if ((tolower(substr(tlf, 1, 1)) %in% "l")) {
615-
ht <- ht %>%
619+
ht <- ht |>
616620
huxtable::set_font_size(value = getOption("tidytlg.fontsize.listing"))
617621
} else {
618-
ht <- ht %>%
622+
ht <- ht |>
619623
huxtable::set_font_size(value = getOption("tidytlg.fontsize.table"))
620624
}
621625

@@ -874,8 +878,8 @@ gentlg_single <- function(huxme = NULL,
874878
getOption("tidytlg.fontsize.table")
875879
) / 10
876880

877-
ht <- huxtable::set_top_border(ht, 1, value = bordervalue) %>%
878-
huxtable::set_bottom_border(1, value = bordervalue) %>%
881+
ht <- huxtable::set_top_border(ht, 1, value = bordervalue) |>
882+
huxtable::set_bottom_border(1, value = bordervalue) |>
879883
huxtable::set_bottom_border(nrow(ht), value = bordervalue)
880884
} else if (is_format_html(format)) {
881885
ht[1, ] <- paste0("<div style='border-top :1pt solid; border-bottom :1pt solid; '> ", ht[1, ])
@@ -904,7 +908,7 @@ gentlg_single <- function(huxme = NULL,
904908
font_size = size, border = 0
905909
)
906910
}
907-
return(dsnin)
911+
dsnin
908912
}
909913
} else if (is_format_html(format)) {
910914
add_footer <- function(dsnin, footer, first = FALSE, size = fontsize) {
@@ -925,7 +929,7 @@ gentlg_single <- function(huxme = NULL,
925929
font_size = size, border = 0
926930
)
927931
}
928-
return(dsnin)
932+
dsnin
929933
}
930934
}
931935

R/replace_lead_whitespaces_ind.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#' Replace leading whitespaces with left indentation RTF markup
2+
#'
3+
#' @details
4+
#' The following function receives a string 'x' and returns a modified string
5+
#' where every leading whitespace is replaced with 90 twips (0.0625 inches) left indentation RTF markup.
6+
#' If the input does not start with a whitespace, the string is returned as is.
7+
#'
8+
#' @param x `character(1)` a string to replace the leading whitespaces.
9+
#'
10+
#' @examples
11+
#' tidytlg:::replace_lead_whitespaces_ind(" this is x")
12+
#' # [1] "\\intbl\\li360\\fi0 this is x"
13+
#' tidytlg:::replace_lead_whitespaces_ind("this is x")
14+
#' # [1] "this is x"
15+
#'
16+
#' @return `character(1)` RTF markup with leading whitespaces replaced.
17+
#' @keywords internal
18+
replace_lead_whitespaces_ind <- function(x) {
19+
# get number of leading whitespaces
20+
num_whitespaces <- attr(regexpr("^\\s*", x), "match.length")
21+
# 2 whitespaces represent an indentation of 0.125 inches = 180 twips
22+
# e.g. 4 whitespaces represent a left-indentation of 0.25 inches = 360 twips
23+
num_twips <- num_whitespaces * 90
24+
if (num_twips > 0) {
25+
raw_rtf_markup <- paste0("\\intbl\\li", num_twips, "\\fi0")
26+
x <- paste0(raw_rtf_markup, " ", trimws(x, "left"))
27+
}
28+
x
29+
}

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ ppapadop
2424
preprocessed
2525
shaesen
2626
tidyverse
27+
twips
2728
vectorized

man/replace_lead_whitespaces_ind.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/borders/stubborntest.rtf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx7367
5757
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx9145
5858
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx10923
59-
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx12701 \pard\intbl\ql\fs18 \keepn\trhdr Standardized medication name \cell
59+
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx12701 \pard\intbl\ql\fs18 \keepn\trhdr \intbl\li90\fi0 Standardized medication name \cell
6060
\pard\intbl\qc\fs18 \keepn\trhdr N=1 \cell
6161
\pard\intbl\qc\fs18 \keepn\trhdr N=1 \cell
6262
\pard\intbl\qc\fs18 \keepn\trhdr N=2 \cell

tests/testthat/test-gentlg.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,20 @@ test_that("custom alignments work", {
4242
expect_equal(align_property_2[4, 2], "center")
4343
expect_equal(align_property_2[3, 3], "right")
4444
})
45+
46+
test_that("replace_lead_whitespaces_ind() is replacing whitespaces", {
47+
# case when there are 2 leading whitespaces, should insert 180 twips
48+
df <- data.frame(label = c("boy", "girl"), name = c("Bob", "Lily"), age = c(12, 15))
49+
expect_no_error(res <- gentlg(huxme = df,
50+
print.hux = FALSE,
51+
colheader = c(" Gender", "Name", "Age")))
52+
res <- as.character(res[[1]][2, 1])
53+
expect_equal(res, "\\keepn\\trhdr \\intbl\\li180\\fi0 Gender")
54+
55+
# case when there are no leading whitespaces, should leave it as is
56+
expect_no_error(res <- gentlg(huxme = df,
57+
print.hux = FALSE,
58+
colheader = c("Gender", "Name", "Age")))
59+
res <- as.character(res[[1]][2, 1])
60+
expect_equal(res, "\\keepn\\trhdr Gender")
61+
})

tests/testthat/test_outputs/borders/stubborntest.rtf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx7367
5757
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx9145
5858
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx10923
59-
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx12701 \pard\intbl\ql\fs18 \keepn\trhdr Standardized medication name \cell
59+
\clbrdrt\clbrdrl\clbrdrb\brdrs\brdrw18\clbrdrr\clvertalb\cellx12701 \pard\intbl\ql\fs18 \keepn\trhdr \intbl\li90\fi0 Standardized medication name \cell
6060
\pard\intbl\qc\fs18 \keepn\trhdr N=1 \cell
6161
\pard\intbl\qc\fs18 \keepn\trhdr N=1 \cell
6262
\pard\intbl\qc\fs18 \keepn\trhdr N=2 \cell

0 commit comments

Comments
 (0)