问题
Is there a smart way to have a horizontal border table wide when you have merged cells? (In the example below, it is not yet table wide).
Or should I write a function to calculate the correct index?
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
回答1:
Here is a code for what you want. It needs more work to be generic - the example is only adapted when column 1 is the only that has merged cells.
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
来源:https://stackoverflow.com/questions/44700492/r-flextable-how-to-add-a-table-wide-horizontal-border-under-a-merged-cell