add_flextable_separators {rtables.officer} | R Documentation |
Add Conditional Separators (horizontal line or padding) to flextable
Rows
Description
Modifies an existing flextable
object by adding visual separators
(horizontal lines or bottom padding) after specific rows based on a
control vector, without changing the table's row count.
Usage
add_flextable_separators(
ft,
trailing_sep,
border = officer::fp_border(width = 1, color = "grey60"),
padding = 10
)
Arguments
ft |
A flextable object. |
trailing_sep |
A vector specifying separators. Its length must equal
the number of rows in the body of |
border |
The |
padding |
The amount of bottom padding (in points) to add when
|
Value
The modified flextable object, or the original ft
if all
trailing_sep
values are NA. Throws an error for invalid inputs or
invalid characters in trailing_sep
.
Examples
content <- data.frame(
USUBJID = c("S1", "S1", "S1", "S2", "S2", "S2", "S3"),
ARM = c("A", "A", "B", "A", "A", "B", "A"),
VAL = round(rnorm(7), 2)
)
ft <- flextable::as_flextable(content)
ft <- flextable::theme_booktabs(ft)
# Define separators: line, space, NA, line, space, NA, NA
sep_ctrl <- c("-", " ", NA, "-", " ", NA, NA)
ft_modified <- add_flextable_separators(ft, sep_ctrl)
print(ft_modified)
# Example: All NA - should return original ft
ft_all_na <- add_flextable_separators(ft, rep(NA, 7))
identical(ft, ft_all_na) # Should be TRUE
# Example: Invalid character - should throw error
tryCatch(
add_flextable_separators(ft, c("-", "x", NA, "-", " ", NA, NA)),
error = function(e) print(e)
)