gpt4 book ai didi

R flextable - 如何在合并单元格下添加表格宽度的水平边框

转载 作者:行者123 更新时间:2023-12-05 02:18:38 34 4
gpt4 key购买 nike

当您合并单元格时,是否有一种聪明的方法可以使表格的水平边框变宽? (在下面的示例中,它还不是表宽)。

或者我应该写一个函数来计算正确的索引吗?

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 列是唯一具有合并单元格的情况下进行调整。

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

关于R flextable - 如何在合并单元格下添加表格宽度的水平边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700492/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com