简体   繁体   中英

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?

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

在此处输入图像描述


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM