![](/img/trans.png)
[英]Table-wide horizontal border under merged cells do not show up in docx (but display fine on other format)
[英]R flextable - How to add a table-wide horizontal border under a merged cell
當您合並單元格時,是否有一種聰明的方法可以使水平邊框表變寬? (在下面的示例中,它還不是表寬的)。
或者我應該寫一個 function 來計算正確的索引?
library(flextable)
library(officer)
library(dplyr)
myft <- flextable(head(mtcars),
col_keys = c("am", "carb", "gear", "mpg", "drat" ))%>%
theme_vanilla()%>%
merge_v(j = c("am"))%>%border(border.bottom = fp_border(style = "solid", width=2), i=c(3,6), part="body")
myft
一個更簡單的解決方案是添加一個列來指示哪些行需要底部邊框,然后添加一個hline()
以及使用該值的行選擇。 通過僅使用col_keys
選擇要在原始彈性表規范中顯示的列,可以將幫助器選擇排除在表之外。
library(tidyverse)
library(flextable)
your_flextable = tibble(
col_group = rep(letters[1:3], each = 3),
the_value = rnorm(length(col_group))
) %>%
group_by(col_group) %>%
mutate(
is_last_val_in_group = row_number() == max(row_number())
) %>%
flextable(col_keys = c('col_group', 'the_value')) %>%
merge_v(j = 'col_group') %>%
hline(i = ~is_last_val_in_group == TRUE, border = fp_border()) %>%
fix_border_issues()
這是您想要的代碼。 泛型需要更多工作-僅當列1是唯一具有合並單元格的示例時,該示例才適用。
library(flextable)
library(officer)
library(dplyr)
bigborder <- fp_border(style = "solid", width=2)
myft <- flextable(head(mtcars),
col_keys = c("am", "carb", "gear", "mpg", "drat" ))%>%
theme_vanilla()%>%
merge_v(j = c("am"))
# here starts the trick
row_loc <- rle(cumsum( myft$body$spans$columns[,1] ))$values
myft <- myft %>%
border(border.bottom = bigborder, i=row_loc, j = 2:5, part="body")
myft <- myft %>%
border(border.bottom = bigborder,
i = myft$body$spans$columns[,1] > 1, j = 1, part="body") %>%
border(border.bottom = bigborder, border.top = bigborder, part = "header")
myft
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.