R flextable-如何在合并的单元格下添加表格范围的水平边框 [英] R flextable - How to add a table-wide horizontal border under a merged cell

查看:596
本文介绍了R flextable-如何在合并的单元格下添加表格范围的水平边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

合并单元格时,是否存在一种聪明的方法来使水平边框表更宽? (在下面的示例中,它还没有表宽).

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

这篇关于R flextable-如何在合并的单元格下添加表格范围的水平边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆