如何在 RMarkdown 中生成带有标题的分页表? [英] How can I generate a paged table with a title in RMarkdown?
问题描述
我有一个 data.frame
,我想以格式化的方式在 RMarkdown 文档(HTML 输出)中打印它.我需要它的 3 个功能,但我无法同时获得:
I have a data.frame
that I want to print in an RMarkdown document (HTML output) in a formatted way. There are 3 features I need for it which I haven't been able to get at the same time:
- 它需要在顶部有一个标题或说明来解释它是什么.
- 由于它可能很长,我希望它可以滚动或分页
- 标题和列标题应始终在顶部可见,即使表格已滚动或分页
<小时>
我可以使用 kableExtra
轻松滚动获得标题:
library(kableExtra)
cars %>%
kable(caption = 'Cars') %>%
kable_styling(bootstrap_options = c('striped', 'condensed')) %>%
scroll_box(width = "500px", height = "200px")
但是当我向下滚动时,标题和列标题也会滚动,而且很难看到每一列是什么.
But when I scroll down, the caption and column headers scroll too, and it's hard to see what each column is.
通过将 df_print: paged
添加到 YAML 标头,我可以获得非常漂亮的分页输出(参见 R Markdown:权威指南)来自默认打印行为:
By adding df_print: paged
to the YAML header, I can get a really nice looking paged output (see R Markdown: The Definitive Guide) from the default printing behavior:
---
title: "Motor Trend Car Road Tests"
output:
html_document:
df_print: paged
---
```{r}
cars
```
但是,据我所知,没有办法在上面加上标题或说明.我可以使用 Markdown 添加标题,但是生成表格的代码会卡在标题和表格之间.我还缺少其他任何选项吗?
But, so far as I know, there is no way to put a title or caption on this. I could just add a title using markdown, but then the code to generate the table would be stuck between the title and the table. Any other options that I'm missing?
推荐答案
我建议尝试 DT::datatable
,它在 html 输出中生成高度可配置的表格.
I would suggest trying DT::datatable
, which produces highly configurable tables in html output.
以下代码将创建一个带有固定标题的可滚动表格.
The following code will create a scrollable table with a fixed header.
DT::datatable(mtcars,
extensions = c('FixedColumns',"FixedHeader"),
options = list(scrollX = TRUE,
paging=FALSE,
fixedHeader=TRUE))
这篇关于如何在 RMarkdown 中生成带有标题的分页表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!