From 85e62276469b33a47f38befed6f4f3a6c2016ed2 Mon Sep 17 00:00:00 2001 From: David Gohel Date: Fri, 3 May 2024 19:07:19 +0200 Subject: [PATCH] fix: `align()` issue with recycling - stop using `match.arg` - fix the documentation fix #623 --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/styles.R | 21 ++++++++++++--------- man/align.Rd | 13 ++++--------- tests/testthat/test-styles.R | 10 ++++++++++ 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index aa3171e8..d34c63df 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: flextable Type: Package Title: Functions for Tabular Reporting -Version: 0.9.6.008 +Version: 0.9.6.009 Authors@R: c( person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"), diff --git a/NEWS.md b/NEWS.md index 4d263678..ccbf6f32 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,9 @@ loops or `if` statements. - fix issue with `as_image()` when the table contains no text. - fix font instruction issue with PDF and quarto - fix issue with Quarto detection and R > 4.4 +- fix `align()` issue with recycling and update documentation +that was wrong about argument `align` that is vectorized over +columns. # flextable 0.9.5 diff --git a/R/styles.R b/R/styles.R index 19f63ab1..7edcb4e9 100644 --- a/R/styles.R +++ b/R/styles.R @@ -601,25 +601,28 @@ padding <- function(x, i = NULL, j = NULL, padding = NULL, #' @param i rows selection #' @param j columns selection #' @param part partname of the table (one of 'all', 'body', 'header', 'footer') -#' @param align text alignment - a single character value, expected value -#' is one of 'left', 'right', 'center', 'justify'. +#' @param align text alignment - character values, expected value +#' must be 'left', 'right', 'center', 'justify'. It can be a single value or +#' multiple values, the argument is vectorized over columns. #' @family sugar functions for table style #' @examples #' ft <- flextable(head(mtcars)[, 3:6]) #' ft <- align(ft, align = "right", part = "all") #' ft <- theme_tron_legacy(ft) #' ft -align <- function(x, i = NULL, j = NULL, align = c("left", "center", "right", "justify"), - part = "body") { +align <- function(x, i = NULL, j = NULL, align = "left", part = "body") { if (!inherits(x, "flextable")) { stop(sprintf("Function `%s` supports only flextable objects.", "align()")) } part <- match.arg(part, c("all", "body", "header", "footer"), several.ok = FALSE) - align_value <- match.arg(align, several.ok = TRUE) + + if (any(!align %in% c("left", "center", "right", "justify"))) { + stop("align values can only be 'left', 'center', 'right', 'justify'.") + } if (part == "all") { for (p in c("header", "body", "footer")) { - x <- align(x = x, i = i, j = j, align = align_value, part = p) + x <- align(x = x, i = i, j = j, align = align, part = p) } return(x) } @@ -632,8 +635,8 @@ align <- function(x, i = NULL, j = NULL, align = c("left", "center", "right", "j i <- get_rows_id(x[[part]], i) j <- get_columns_id(x[[part]], j) - if (length(align_value) == length(j)) { - align_value <- rep(align_value, each = length(i)) + if (length(align) == length(j)) { + align <- rep(align, each = length(i)) } x[[part]]$styles$pars <- set_par_struct_values( @@ -641,7 +644,7 @@ align <- function(x, i = NULL, j = NULL, align = c("left", "center", "right", "j i = i, j = j, property = "text.align", - value = align_value + value = align ) x } diff --git a/man/align.Rd b/man/align.Rd index 37d6a02b..63312d16 100644 --- a/man/align.Rd +++ b/man/align.Rd @@ -6,13 +6,7 @@ \alias{align_nottext_col} \title{Set text alignment} \usage{ -align( - x, - i = NULL, - j = NULL, - align = c("left", "center", "right", "justify"), - part = "body" -) +align(x, i = NULL, j = NULL, align = "left", part = "body") align_text_col(x, align = "left", header = TRUE, footer = TRUE) @@ -25,8 +19,9 @@ align_nottext_col(x, align = "right", header = TRUE, footer = TRUE) \item{j}{columns selection} -\item{align}{text alignment - a single character value, expected value -is one of 'left', 'right', 'center', 'justify'.} +\item{align}{text alignment - character values, expected value +must be 'left', 'right', 'center', 'justify'. It can be a single value or +multiple values, the argument is vectorized over columns.} \item{part}{partname of the table (one of 'all', 'body', 'header', 'footer')} diff --git a/tests/testthat/test-styles.R b/tests/testthat/test-styles.R index f25835b5..65aa8c7c 100644 --- a/tests/testthat/test-styles.R +++ b/tests/testthat/test-styles.R @@ -84,3 +84,13 @@ test_that("borders with office docs are sanitized", { expect_equal(xml_attr(top_nodes, "w"), c("0", "0", "0", "12700")) expect_equal(xml_attr(bot_nodes, "w"), c("0", "12700", "0", "12700")) }) + +test_that("align is vectorized over columns", { + ft <- flextable(head(mtcars[, 2:6])) + align_vals <- c("center", "right", "right", "right", "right") + ft <- align(ft, align = align_vals, part = "all") + expect_equal( + rep(align_vals, 7), + information_data_paragraph(ft)$text.align + ) +})