我有一个四开本文档,其中包含多个表格。我在整个文档中一直使用 R,但我想要一个表格(
gt
包)可以滚动,因此经过一番研究,我使用 CSS 添加了下面的代码来实现这一目标:
gt() %>%
tab_options(
table.width = "1000px", # Set table width
table.additional_css = "
tbody {
display: block;
overflow-y: scroll;
max-height: 400px;
}
thead, tbody tr {
display: table;
width: 700px;
table-layout: fixed;
}
"
)
这非常有效,只是文档中的每个表格都受到影响并变得可滚动,这是我不希望的。我仅将上面的代码添加到我想要更改的一个表的代码块中,尽管调整了宽度大小,但它似乎仍然是一个全局命令。
我当前的文档标题是:
format:
html:
embed-resources: true
toc: true
code-fold: true
code-tools: true
我需要在标题中添加全局命令吗?我之前没有使用过 CSS,但我无法在网上找到修复方法。预先感谢!
解决问题的一个选项是在表格中添加
id=
,这样可以将 CSS 修改仅定位到所需的表格:
---
format: html
---
```{r}
library(gt)
```
```{r}
gt(gtcars, id = "scroll") |>
tab_options(
table.width = "1000px",
table.additional_css = "
#scroll tbody {
display: block;
overflow-y: scroll;
max-height: 400px;
}
#scroll thead, tbody tr {
display: table;
width: 700px;
table-layout: fixed;
}
"
)
```
```{r}
head(gtcars, 10) |>
gt()
```