问题
I'm trying to make a 'compact' table in an RMarkdown
I've tried a few things, mostly variations on setting a custom css class and providing the custom css class to a code chunk
I've tried a lot of variations, all of which I can see flow through to the source code (accessed via knitting the html document, opening in chrome, and cmd + opt + u to view source and inspecting the source)
However, I can't work out what's necessary to simply make rows thinner (I believe that's simply reducing cell padding) in a kableExtra
table
What I've tried so far
Here's one variation of what I've tried, but the rows are not compact as hoped (they are the standard height)
Which is done with:
---
output: html_document
---
```{r setup, include=FALSE}
library(dplyr); library(kableExtra)
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(dplyr)
library(kableExtra)
```
<style>
pre code, pre, code {
padding: 200 !important;
}
</style>
```{r}
iris %>%
kable %>%
kable_styling("striped", full_width = F) %>%
column_spec(4:5, bold = T) %>%
row_spec(3:5, bold = T, color = "white", background = "#D7261E")
```
but note that the custom css is not taking effect
回答1:
The easiest way is to override the Bootstrap CSS, decreasing value of padding
property (default value is 8px
):
<style>
.table>tbody>tr>td{
padding: 1px;
}
</style>
As you pointed out, inspecting the source will lead you to the values above:
回答2:
You could also do something similar within row_spec(1:nrow(iris), extra_css = "..")
来源:https://stackoverflow.com/questions/61378665/compact-table-in-html-document-rmarkdown-with-kableextra