Skip to content

Commit

Permalink
fix: align() issue with recycling
Browse files Browse the repository at this point in the history
- stop using `match.arg`
- fix the documentation

fix #623
  • Loading branch information
davidgohel committed May 3, 2024
1 parent b975e1c commit 85e6227
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 12 additions & 9 deletions R/styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -632,16 +635,16 @@ 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(
x = x[[part]]$styles$pars,
i = i,
j = j,
property = "text.align",
value = align_value
value = align
)
x
}
Expand Down
13 changes: 4 additions & 9 deletions man/align.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})

0 comments on commit 85e6227

Please sign in to comment.