使用 collapse_row 和 kable 时,如何解决 pdf 文档中长 table 的 alignment/display 问题?
How to fix alignment/display problems of a long table in a pdf document when using collapse_row and kable?
我必须生成具有相当长 table 的数据列表,这些列表必须显示在多个页面上,并且我想折叠一些行(例如:ID)。
我正在使用包 kable
生成 table 和 collapse_row
函数来折叠我的 ID 行。
当我编织成 pdf 时,列中折叠行的对齐方式完全混乱,我什至让 ID 出现在 table 之外或重复出现 header.. .
这是我的代码:
kable(data, "latex", booktabs = T,longtable = T, linesep = "", row.names = FALSE) %>%
kable_styling(full_width = T,
position = "center",
latex_options = c("striped", "repeat_header","hold_position"),
font_size = 6,5)%>%
column_spec(c(1), color = "black", width = "5em")%>%
column_spec(c(2), color = "black", width = "30em")%>%
collapse_rows(1, latex_hline = "custom",valign = "top")%>%
row_spec(0, bold = T, color = "black")
这是我的 pdf 输出的一部分:
线条很好,但 ID 移到了顶部。
我该如何解决这个问题?
谢谢。
看起来像是表格中垂直对齐的 LaTeX 问题。
引自 github 上的问题:
[...]it seems like booktabs' cmidrule messed up the multirow's calculation..
[...] I don't know if I can find a good solution for this. I can certainly remove those midlines. I don't like them either but they are important for tables with multiple header columns. If we keep those midlines, TeX people say you can manually add adjustments to multirow. I can certainly simulate it via code but it seems like it will bring me more issues in the future.
来源:https://github.com/haozhu233/kableExtra/issues/56
其他参考资料:
- https://github.com/hughjonesd/huxtable/issues/9
- https://hughjonesd.github.io/huxtable/design-principles.html(“限制”部分)
- https://tex.stackexchange.com/questions/66564/vertical-alignment-using-multirow-and-booktabs
This solution worked well in my case when I had the same problem. The solution is based on manipulating the data.frame before outputting to kable rather than changing the kable output with collapse_rows. The original contributor (Michael Harper) 开发它是为了与 formattable 包一起使用,但它可以很容易地针对 kable 包进行调整。
您需要声明此函数:
collapse_rows_df <- function(df, variable){
group_var <- enquo(variable)
df %>%
group_by(!! group_var) %>%
mutate(groupRow = 1:n()) %>%
ungroup() %>%
mutate(!!quo_name(group_var) := ifelse(groupRow == 1, as.character(!! group_var), "")) %>%
select(-c(groupRow))
}
该函数去除指定列中除第一次遇到的数据以外的所有数据,而是保留空格。
您需要在将数据发送到kable 之前调用该函数。请参阅以下示例:
library(knitr) # required for kable()
# dfToKable - your data.frame you need to kable
# columnToCollapseRows - the name of the column, in which you want to collapse the rows;
# it should be a valid variable within the data.frame set and be passed without quotes
kable( collapse_rows_df( dfToKable, columnToCollapseRows ),
row.names = F, escape = F, longtable = TRUE) %>%
kable_styling(full_width = T, latex_options = c("repeat_header"))
根据您提供的代码和屏幕截图,这应该适合您:
kable( collapse_rows_df( data, SubID) "latex", booktabs = T,longtable = T, linesep = "", row.names = FALSE) %>%
kable_styling(full_width = T,
position = "center",
latex_options = c("striped", "repeat_header","hold_position"),
font_size = 6,5)%>%
column_spec(c(1), color = "black", width = "5em")%>%
column_spec(c(2), color = "black", width = "30em")%>%
row_spec(0, bold = T, color = "black")
我必须生成具有相当长 table 的数据列表,这些列表必须显示在多个页面上,并且我想折叠一些行(例如:ID)。
我正在使用包 kable
生成 table 和 collapse_row
函数来折叠我的 ID 行。
当我编织成 pdf 时,列中折叠行的对齐方式完全混乱,我什至让 ID 出现在 table 之外或重复出现 header.. .
这是我的代码:
kable(data, "latex", booktabs = T,longtable = T, linesep = "", row.names = FALSE) %>%
kable_styling(full_width = T,
position = "center",
latex_options = c("striped", "repeat_header","hold_position"),
font_size = 6,5)%>%
column_spec(c(1), color = "black", width = "5em")%>%
column_spec(c(2), color = "black", width = "30em")%>%
collapse_rows(1, latex_hline = "custom",valign = "top")%>%
row_spec(0, bold = T, color = "black")
这是我的 pdf 输出的一部分:
我该如何解决这个问题? 谢谢。
看起来像是表格中垂直对齐的 LaTeX 问题。
引自 github 上的问题:
[...]it seems like booktabs' cmidrule messed up the multirow's calculation..
[...] I don't know if I can find a good solution for this. I can certainly remove those midlines. I don't like them either but they are important for tables with multiple header columns. If we keep those midlines, TeX people say you can manually add adjustments to multirow. I can certainly simulate it via code but it seems like it will bring me more issues in the future.
来源:https://github.com/haozhu233/kableExtra/issues/56
其他参考资料:
- https://github.com/hughjonesd/huxtable/issues/9
- https://hughjonesd.github.io/huxtable/design-principles.html(“限制”部分)
- https://tex.stackexchange.com/questions/66564/vertical-alignment-using-multirow-and-booktabs
This solution worked well in my case when I had the same problem. The solution is based on manipulating the data.frame before outputting to kable rather than changing the kable output with collapse_rows. The original contributor (Michael Harper) 开发它是为了与 formattable 包一起使用,但它可以很容易地针对 kable 包进行调整。
您需要声明此函数:
collapse_rows_df <- function(df, variable){
group_var <- enquo(variable)
df %>%
group_by(!! group_var) %>%
mutate(groupRow = 1:n()) %>%
ungroup() %>%
mutate(!!quo_name(group_var) := ifelse(groupRow == 1, as.character(!! group_var), "")) %>%
select(-c(groupRow))
}
该函数去除指定列中除第一次遇到的数据以外的所有数据,而是保留空格。
您需要在将数据发送到kable 之前调用该函数。请参阅以下示例:
library(knitr) # required for kable()
# dfToKable - your data.frame you need to kable
# columnToCollapseRows - the name of the column, in which you want to collapse the rows;
# it should be a valid variable within the data.frame set and be passed without quotes
kable( collapse_rows_df( dfToKable, columnToCollapseRows ),
row.names = F, escape = F, longtable = TRUE) %>%
kable_styling(full_width = T, latex_options = c("repeat_header"))
根据您提供的代码和屏幕截图,这应该适合您:
kable( collapse_rows_df( data, SubID) "latex", booktabs = T,longtable = T, linesep = "", row.names = FALSE) %>%
kable_styling(full_width = T,
position = "center",
latex_options = c("striped", "repeat_header","hold_position"),
font_size = 6,5)%>%
column_spec(c(1), color = "black", width = "5em")%>%
column_spec(c(2), color = "black", width = "30em")%>%
row_spec(0, bold = T, color = "black")