Reputation: 20389
Is there a way to set up default kableExtra
stylings to be applied to each table in an Rmarkdown
document in order to avoid typing the same sytling options over and over again?
In the reprex below, you see that I have to add kable_styling(c("striped", "hover", "condensed", "responsive"))
to each kable I want to produce, so I was wondering whether there is maybe an option which allows to define the default styling?
---
title: "Default Kables"
output: html_document
---
```{r setup}
library(kableExtra)
```
```{r mtcars}
mtcars %>%
head() %>%
kable() %>%
kable_styling(c("striped", "hover", "condensed", "responsive"))
```
```{r iris}
iris %>%
head() %>%
kable() %>%
kable_styling(c("striped", "hover", "condensed", "responsive"))
```
Of course there is a trivial solution to define a helper function like this:
kable <- function(...) {
knitr::kable(...) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"))
}
but I was wondering whether there is a dedicated option for that?
Upvotes: 2
Views: 996
Reputation: 1059
I'm looking into this and there seems to be a bunch of global options "hidden" in the main functions' source code. Searching for getOption(*)
in knitr/R/table.R
and kableExtra/R/kable_styling.R
reveals:
knitr::kable()
General
digits
— not specific to knitr
/kable
knitr.kable.NA
HTML
knitr.table.html.attr
LaTeX
knitr.table.vline
knitr.table.toprule
knitr.table.bottomrule
knitr.table.midrule
kableExtra::kable_styling()
General
kable_styling_position
— default: "center"
kable_styling_font_size
kable_styling_full_width
— default: TRUE
for HTML and FALSE
for LaTeXHTML
kable_styling_bootstrap_options
— default: "basic"
LaTeX
kable_styling_latex_options
— default: "basic"
Note: All of them seem to be unset in options()
by default.
To apply kable_styling()
by default, you can set this option in the YAML header:
output:
html:
df_print: !expr function(x, ...) kable(x, ...) %>% kableExtra::kable_styling() %>% knit_print()
Upvotes: 2
Reputation: 20017
You can try setting the bootstrap options globally, although you still need to call kable_styling
repeatedly.
---
title: "Default Kables"
output: html_document
---
```{r setup, include=FALSE}
library(kableExtra)
bs_style <- c("striped", "hover", "condensed", "responsive")
options(kable_styling_bootstrap_options = bs_style)
```
```{r mtcars}
mtcars %>%
head() %>%
kable() %>%
kable_styling()
```
Upvotes: 3