0

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)

enter image description here

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

stevec
  • 41,291
  • 27
  • 223
  • 311

3 Answers3

3

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: enter image description here

Radovan Miletić
  • 2,521
  • 1
  • 7
  • 13
1

You could also do something similar within row_spec(1:nrow(iris), extra_css = "..")

stevec
  • 41,291
  • 27
  • 223
  • 311
Tom Cotter
  • 19
  • 3
1

To make the kable rows smaller in a knit to HTML, you can use bootstrap_options = c("condensed") in your kable_styling:

kable_styling(bootstrap_options = c("condensed"))

See https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html

If anyone knows the pdf variant for it, please let me know :)

MartineJ
  • 591
  • 5
  • 16