Changing the style of one table in Rmarkdown with kable

I am writing a vignette using knitr and rmarkdown, choosing the rmarkdown :: html_vignette style. Most of my tables are injected as table markup tables, but I use kable()

one for one.

Generally, I like the default style for tables, but in one particular table (out of several) I would like to suppress odd-even line shading.

Is there an easy way to reverse the CSS

table thead, table tr.even {
  background-color: #f7f7f7;
}

      

just for one specific table generated kable

?

Here's an example of a file shaded for both tables. I only need one thing:

---
output: rmarkdown::html_vignette
---

This table should have alternate shading:
```{r output="asis"}
library(knitr)
kable(matrix(1:20, nrow=5))
```

How do I turn it off for just this one?
```{r output="asis"}
kable(matrix(1:20, nrow=5))
```

      

+3


source to share


1 answer


Here is a partial answer to the question. It's ugly; I hope someone else will be better.

One solution is to define a new style for a specific id or CSS class, and then wrap the desired table in <DIV id="newid"> </DIV>

or <DIV class="newclass"> </DIV>

. As far as I can tell, there is no way to do this on call kable()

, so it needs to be put right in the text.

It looks like the style itself should be placed in the text as well as in the block <STYLE></STYLE>

. While the header allows you to indicate css

, as far as I can tell, that it can only replace an existing style file, you cannot add to it.



So here's my ugly solution:

---
output: 
  rmarkdown::html_vignette
---

This table should have alternate shading:
```{r output="asis"}
library(knitr)
kable(matrix(1:20, nrow=5))
```

Define a style for class "nostripes", then wrap tables in a DIV with 
class "nostripes" to turn it off:


<style>
.nostripes tr.even {background-color: white;}
</style>
<div class="nostripes">
```{r output="asis"}
kable(matrix(1:20, nrow=5))
```
</div>

      

+3


source







All Articles