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 add_counts_to_same_line to set where to put (N=xx), if same line or new line #847

Closed
wants to merge 11 commits into from
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* Fixed edge case bug in `as_result_df` where rows of the table have only `"root"` as path index.
* Fixed `sort_at_path` pathing to ignore leading `"root"` element (regardless of actual root element name) to match current `tt_at_path` behavior.
* Fixed `section_div` for analysis of multiple variables (`AnalyzeMultiVars`).

### Miscellaneous
* Added `add_counts_to_same_line` to `tt_to_flextable()` to be able to set the position of the column counts.

### Miscellaneous
* Removed deprecated functions `add_analyzed_var` and `trim_zero_rows`.
Expand Down
9 changes: 4 additions & 5 deletions R/tt_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ margins_landscape <- function() {
#' object to change its layout and style. If `NULL`, it will produce a table similar to `rtables` default. Defaults
#' to `theme_docx_default(tt)`.
#' @param border (`officer` border object)\cr defaults to `officer::fp_border(width = 0.5)`.
#' @param counts_in_newline (`flag`)\cr defaults to `TRUE` for printing the counts on a new line. Otherwise,
#' `FALSE` will print the counts in the same line as the column name (see [add_colcounts()]).
#' @param indent_size (`integer(1)`)\cr if `NULL`, the default indent size of the table (see [matrix_form()]
#' `indent_size`) is used. To work with `docx`, any size is multiplied by 2 mm (5.67 pt) by default.
#' @param titles_as_header (`flag`)\cr defaults to `TRUE` for [tt_to_flextable()], so the table is self-contained
Expand All @@ -750,9 +752,6 @@ margins_landscape <- function() {
#' @param footers_as_text (`flag`)\cr defaults to `FALSE` for [tt_to_flextable()], so the table is self-contained with
#' the `flextable` definition of footnotes. `TRUE` is used for [export_as_docx()] to add the footers as a new
#' paragraph after the table. The same style is applied, but with a smaller font.
#' @param counts_in_newline (`flag`)\cr defaults to `FALSE`. In `rtables` text printing ([formatters::toString()]),
#' the column counts, i.e. `(N=xx)`, are always on a new line. For `docx` exports it could be necessary to print it
#' on the same line.
#' @param paginate (`flag`)\cr when exporting `.docx` documents using `export_as_docx`, we suggest relying on the
#' Microsoft Word pagination system. If `TRUE`, this option splits `tt` into different "pages" as multiple
#' `flextables`. Cooperation between the two mechanisms is not guaranteed. Defaults to `FALSE`.
Expand Down Expand Up @@ -856,10 +855,10 @@ tt_to_flextable <- function(tt,
# IMPORTANT: Fix of (N=xx) which is by default on a new line but we usually do not
# want this, and it depends on the size of the table, it is not another
# row with different columns -> All of this should be fixed at source (in toString)
if (hnum > 1) { # otherwise nothing to do
if (hnum > 1) { # otherwise nothing to do (it is one line header already)
det_nclab <- apply(hdr, 2, grepl, pattern = "\\(N=[0-9]+\\)$")
has_nclab <- apply(det_nclab, 1, any)
if (isFALSE(counts_in_newline) && any(has_nclab)) {
if (!counts_in_newline && any(has_nclab)) {
whsnc <- which(has_nclab) # which rows have it
what_is_nclab <- det_nclab[whsnc, ]

Expand Down
5 changes: 2 additions & 3 deletions man/tt_to_flextable.Rd

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

2 changes: 0 additions & 2 deletions tests/testthat/setup-fakedata.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
## Loading relevant libraries for tests
library(testthat)
library(xml2)
library(tibble)
library(rtables)
library(dplyr)

# # Load and flag for pdftools to check for it
Expand Down
27 changes: 24 additions & 3 deletions tests/testthat/test-exporters.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ test_that("export_as_rtf works", {

# Flextable and docx support ---------------------------------------------------
test_that("Can create flextable object that works with different styles", {
skip_if_not_installed(c("flextable", "officer"))
analysisfun <- function(x, ...) {
in_rows(
row1 = 5,
Expand Down Expand Up @@ -374,14 +375,14 @@ test_that("Can create flextable object that works with different styles", {
topleft_t1 <- topleft_t1 %>%
analyze("BMRKR1") %>%
build_table(DM)
topleft_t1a <- tt_to_flextable(topleft_t1, counts_in_newline = FALSE)
topleft_t1b <- tt_to_flextable(topleft_t1, counts_in_newline = TRUE)
topleft_t1a <- tt_to_flextable(topleft_t1, add_counts_to_same_line = TRUE)
topleft_t1b <- tt_to_flextable(topleft_t1, add_counts_to_same_line = FALSE)

topleft_t2 <- topleft_t2 %>%
split_rows_by("SEX", label_pos = "topleft") %>%
analyze("BMRKR1") %>%
build_table(DM) %>%
tt_to_flextable(counts_in_newline = FALSE)
tt_to_flextable(add_counts_to_same_line = TRUE)

expect_equal(flextable::nrow_part(topleft_t2, part = "header"), 2L)
expect_equal(flextable::nrow_part(topleft_t1a, part = "header"), 1L)
Expand All @@ -394,6 +395,7 @@ test_that("Can create flextable object that works with different styles", {
})

test_that("export_as_doc works thanks to tt_to_flextable", {
skip_if_not_installed(c("flextable", "officer"))
lyt <- make_big_lyt()
tbl <- build_table(lyt, rawdat)
top_left(tbl) <- "Ethnicity"
Expand Down Expand Up @@ -431,3 +433,22 @@ test_that("export_as_doc works thanks to tt_to_flextable", {

expect_true(file.exists(doc_file))
})

test_that("tt_to_flextable works with add_counts_to_same_line", {
skip_if_not_installed(c("flextable", "officer"))
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ARM") %>%
analyze("BMRKR1")
tbl <- build_table(lyt, DM)

expect_silent(flx <- tt_to_flextable(tbl, add_counts_to_same_line = FALSE))
expect_equal(flextable::nrow_part(flx, part = "header"), 2)
expect_silent(flx <- tt_to_flextable(tbl, add_counts_to_same_line = TRUE))
expect_equal(flextable::nrow_part(flx, part = "header"), 1)

lyt <- basic_table(show_colcounts = FALSE) %>%
split_cols_by("ARM") %>%
analyze("BMRKR1")
tbl <- build_table(lyt, DM)
expect_warning(flx <- tt_to_flextable(tbl, add_counts_to_same_line = FALSE))
})