[英]Way to merge flextables and retain formatting
說我有2個flextables:
ft1 <- regulartable(head(iris))
ft2 <- regulartable(tail(iris))
他們有不同的格式:
ft1 <- bg(ft1, bg="green")
ft2 <- color(ft2, color = "blue")
有沒有辦法在它們已經是flextables之后合並這兩個,並保持格式化?
我可以使用它合並它們:
ft3 <- regulartable(rbind(ft1$body$dataset, ft2$body$dataset))
但我失去了所有的格式。
我知道在轉換為flextables之前合並數據幀會更容易,但是我生成實際數據的方式很難實現,因為我試圖合並的兩個flextables是我編寫的其他函數的結果。
編輯:
目的是保持個人格式,如下所示:
不是很優雅,但可能會完成工作......
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.