我正在尝试使用R Markdown在HTML中生成一个弹性表,该表具有多个单元格,这些单元格使用ReporteRs::spanFlexTableRows()
跨多行合并。但是,当我将单元格合并在一起时,合并单元格中显示的数字将显示在单元格的顶部。而且,两组(相邻)合并单元格之间的边界消失了。我想知道:
以下R脚本说明了我遇到的问题:
library(tidyverse)
library(ReporteRs)
mtcars[1:4,] %>%
mutate(
x = row.names(mtcars[1:4,]),
var1 = 1,
var2 = 2
) %>%
select(
x, mpg, cyl, disp, var1, var2
) %>%
make_ft %>%
spanFlexTableRows(j=5, from=1, to= 4) %>%
spanFlexTableRows(j=6, from=1, to= 4)
感谢您的帮助!
答案 0 :(得分:1)
我有一个密切相关的问题的解决方案-使用人员/弹性表而不是ReporteR进行合并和垂直对齐。由于官员/灵活表旨在替换ReporteR,因此我认为将其发布在此处是合理的。
可以使用valign()在flextable的github版本(截至2019-02-10)中直观地进行垂直对齐:
library(tidyverse)
library(officer)
library(flextable)
mtcars[1:4,] %>%
mutate(
x = row.names(mtcars[1:4,]),
var1 = 1,
var2 = 2
) %>%
select(
x, mpg, cyl, disp, var1, var2
) %>%
flextable() %>%
flextable::merge_at(j=5, i=1:4) %>%
flextable::merge_at(j=6, i=1:4) %>%
valign(j = 5:6, valign = 'top') -> myft
myft
flextable中的垂直对齐历史上(自2019年2月10日CRAN版本开始)是flextable :: rotate()函数的一部分。这是您的示例:
library(tidyverse)
# library(ReporteRs)
library(officer)
library(flextable)
# turn into flextable, merge and apply a vertical alignment
# (note that center vertical alignment appears to be the default in flextables
# so despite your goals i do top alignment here)
mtcars[1:4,] %>%
mutate(
x = row.names(mtcars[1:4,]),
var1 = 1,
var2 = 2
) %>%
select(
x, mpg, cyl, disp, var1, var2
) %>%
flextable() %>%
flextable::merge_at(j=5, i=1:4) %>%
flextable::merge_at(j=6, i=1:4) %>%
rotate(j = 5:6, align = 'top', rotation = 'tblr') -> myft
# write to a docx using officer
doc = read_docx()
doc = flextable::body_add_flextable( doc, myft )
print(doc, target = "ftex.docx" )