Say I have 2 flextables:
ft1 <- regulartable(head(iris))
ft2 <- regulartable(tail(iris))
And they have distinct formatting:
ft1 <- bg(ft1, bg="green")
ft2 <- color(ft2, color = "blue")
Is there a way to merge these two after they are already flextables, and keep the formatting?
I can merge them using this:
ft3 <- regulartable(rbind(ft1$body$dataset, ft2$body$dataset))
but I lose all formatting.
I understand it would be easier to merge the dataframes before converting to flextables, but the way my actual data is generated makes that difficult as the two flextables I'm trying to merge are the result of other functions I've written.
Edit:
The aim is to keep the individual formatting, like this:
Not very elegant but might get the job done...
library(flextable)
library(magrittr)
ft1 <- regulartable(head(iris))
ft2 <- regulartable(tail(iris))
ft_formatting <- function(ft1, ft2,
color1 = "black", bg1 = "white", color2 = "black", bg2 = "white") {
n_row1 <- nrow(ft1$body$dataset)
n_row2 <- nrow(ft2$body$dataset)
n_col1 <- ncol(ft1$body$dataset)
n_col2 <- ncol(ft2$body$dataset)
i_1 <- 1:n_row1
i_2 <- n_row1+1:n_row2
regulartable(rbind(ft1$body$dataset, ft2$body$dataset)) %>%
bg(i = i_1, j = 1:5, bg = bg1) %>%
color(i = i_1, j = 1:n_col1, color = color1) %>%
bg(i = i_2, j = 1:5, bg = bg2) %>%
color(i = i_2, j = 1:n_col2, color = color2)
}
ft_formatting(ft1, ft2, bg1 = "green", color2 = "blue")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With