Search code examples
rr-markdownkablekableextra

Option to specify default kableExtra Styling in Rmarkdown


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?


Solution

  • 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()
    ```
    

    enter image description here