问题描述
上下文:使用描述的技巧here我能够在合并的单元格下获得水平线。
问题:当我使用 save_as_docx()
时,第一列(合并单元格的列)的底线不显示。另外,最后一行底线的格式不正确。
但是,如果我使用 .pptx、.html 或 .png 输出中的任何一个,它都可以正常工作。
问题:是否可以使 .docx 输出具有全宽底线,即使在合并的单元格下也具有正确的格式?
Reprex:
library(flextable)
library(officer)
library(dplyr)
bigborder <- fp_border(style = "solid",width=2)
fl <- iris %>%
# make iris a smaller dataset for display purpose
group_by(Species) %>%
slice_head(n = 5) %>%
# just to have species as the 1st column
select(Species,everything()) %>%
flextable() %>%
merge_v(j = ~ Species) %>%
# bottom border of column 2:ncol(iris)
border(border.bottom = bigborder,i = rle(cumsum(.$body$spans$columns[,1] ))$values,j = 2:ncol(iris),part="body") %>%
# bottom border of 1st colum
border(border.bottom = bigborder,i = .$body$spans$columns[,1] > 1,j = 1,part="body")
fl
# Bottom lines of 1st column do not display with docx output
save_as_docx(fl,path = "./fl.docx")
# Bottom lines of 1st column display right with these format
save_as_html(fl,path = "./fl.html")
save_as_pptx(fl,path = "./fl.pptx")
save_as_image(fl,path = "./fl.png")
由 reprex package (v2.0.0) 于 2021 年 6 月 2 日创建
.docx 输出:
解决方法
刚刚使用 hline()
和 fix_border_issues()
获得预期的输出here
fl <- iris %>%
# make iris a smaller dataset for display purpose
group_by(Species) %>%
slice_head(n = 5) %>%
# just to have species as the 1st column
select(Species,everything()) %>%
# Flextable
flextable() %>%
merge_v(j = ~ Species) %>%
hline(i = rle(cumsum(.$body$spans$columns[,1] ))$values,border = bigborder) %>%
fix_border_issues()
fl
.docx 输出: