diff --git a/NEWS.md b/NEWS.md index d3c8465ec..862678e10 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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`. diff --git a/R/tt_export.R b/R/tt_export.R index 6583f73cc..c88b334cd 100644 --- a/R/tt_export.R +++ b/R/tt_export.R @@ -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 @@ -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`. @@ -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, ] diff --git a/man/tt_to_flextable.Rd b/man/tt_to_flextable.Rd index 075e9355e..8c67a81fd 100644 --- a/man/tt_to_flextable.Rd +++ b/man/tt_to_flextable.Rd @@ -53,9 +53,8 @@ The same style is applied.} the \code{flextable} definition of footnotes. \code{TRUE} is used for \code{\link[=export_as_docx]{export_as_docx()}} to add the footers as a new paragraph after the table. The same style is applied, but with a smaller font.} -\item{counts_in_newline}{(\code{flag})\cr defaults to \code{FALSE}. In \code{rtables} text printing (\code{\link[formatters:tostring]{formatters::toString()}}), -the column counts, i.e. \code{(N=xx)}, are always on a new line. For \code{docx} exports it could be necessary to print it -on the same line.} +\item{counts_in_newline}{(\code{flag})\cr defaults to \code{TRUE} for printing the counts on a new line. Otherwise, +\code{FALSE} will print the counts in the same line as the column name (see \code{\link[=add_colcounts]{add_colcounts()}}).} \item{paginate}{(\code{flag})\cr when exporting \code{.docx} documents using \code{export_as_docx}, we suggest relying on the Microsoft Word pagination system. If \code{TRUE}, this option splits \code{tt} into different "pages" as multiple diff --git a/tests/testthat/setup-fakedata.R b/tests/testthat/setup-fakedata.R index f22111193..08abc4afc 100644 --- a/tests/testthat/setup-fakedata.R +++ b/tests/testthat/setup-fakedata.R @@ -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 diff --git a/tests/testthat/test-exporters.R b/tests/testthat/test-exporters.R index ec10f6793..017706a29 100644 --- a/tests/testthat/test-exporters.R +++ b/tests/testthat/test-exporters.R @@ -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, @@ -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) @@ -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" @@ -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)) +})