本文介绍了使用 kable() 生成跨越多个页面的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 kable() 生成一个跨越多个页面的表格.我知道使用带有longtable"选项的 xtable() 是可能的,但我需要 kable() 来实现其他功能.

I would like to produce a table that spans over multiple pages using kable(). I know this is possible using xtable() with the "longtable" option, but I need kable() for other features.

有什么想法吗?

```{r cars, echo=TRUE, results='asis', warning=FALSE, message=FALSE}
    library(knitr)
    library(kableExtra)

# OUTPUT 1, fits on one page
output = rbind(mtcars[, 1:5])

kable(output, booktabs = T, format="latex", caption = "Small Output")


# OUTPUT 2, will not fit on one page
output = rbind(mtcars[, 1:5], mtcars[, 1:5])

kable(output, booktabs = T, format="latex", caption = "Large Output")

```

更新:我傻了!longtable=TRUE"是一个选项.问题是这改变了我的输出顺序并且有点搞砸了.

Update: I am dumb! "longtable=TRUE," is an option. The problem is that this changes the order of my output and kinda messes things up.

推荐答案

您可以尝试使用 kableExtra 包.如果您在 kable_styling 中指定 hold_position,您应该能够 ping 表到您想要的位置.

You can try to use the kableExtra package. If you specify hold_position in kable_styling, you should be able to ping the table to the place you want.

此外,在当前的开发版本中,我为 longtable 引入了一个名为 repeat_header 的新功能,用于在每个页面上重复标题行.你可以检查一下.

Also, in the current dev version, I introduced a new feature called repeat_header for longtable to repeat the header row on every page. You can check it out.

kable(output, "latex", booktabs = TRUE, longtable = TRUE, caption = "Test") %>%
  kable_styling(latex_options = c("hold_position", "repeat_header"))

这篇关于使用 kable() 生成跨越多个页面的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:16
查看更多