I am using xtable
in R with knitr
to produce some nice looking tables. I would like to use CSS that makes us of <thead>
.
Problem is that with xtable
, I get <th>
wrapped in <tr>
, but nothing else, so the HTML code from RMarkdown tables and xtables look different from each other.
I can change the css, but I would rather not since it's used for other things as well - I would especially like to use the same CSS with RMarkdown tables as for xtable-tables.
Here's my code (in test.Rmd)
```{r, comment=NA, results="asis", tidy=TRUE, echo=TRUE, message=FALSE, warning=FALSE}
require(xtable)
options(xtable.type = 'html')
xtable( mtcars )
```
| Lorem ipsum dolor sit amet. | Lorem ipsum dolor sit amet. |
|-----------------------------|-----------------------------|
| Lorem ipsum dolor sit amet. | Lorem ipsum dolor sit amet. |
Then I run
Rscript -e "library(knitr); knit2html('test.Rmd')"
which produces 'test.md' in which the table header looks like this:
<!-- html table generated in R 3.1.1 by xtable 1.7-3 package -->
<!-- Wed Sep 17 09:53:11 2014 -->
<TABLE border=1>
<TR> <TH> </TH> <TH> mpg </TH> <TH> cyl </TH> <TH> disp </TH> <TH> hp </TH> <TH> drat </TH> <TH> wt </TH> <TH> qsec </TH> <TH> vs </TH> <TH> am </TH> <TH> gear </TH> <TH> carb </TH> </TR>
I would however like to get something like what is generated from the RMarkdown table above.
<table><thead>
<tr>
<th>Lorem ipsum dolor sit amet.</th>
<th>Lorem ipsum dolor sit amet.</th>
</tr>
</thead><tbody>
<tr>
<td>Lorem ipsum dolor sit amet.</td>
<td>Lorem ipsum dolor sit amet.</td>
</tr>
</tbody></table>
The question
How can I add <thead>
and <tbody>
to xtable output so align RMarkdown tables and xtablesx?
Use kable
instead of xtable
to get the output you describe:
```{r, comment=NA, results="asis", tidy_source=TRUE, echo=TRUE, message=FALSE, warning=FALSE}
require(knitr)
kable( mtcars, format="html")
```
| Lorem ipsum dolor sit amet. | Lorem ipsum dolor sit amet. |
|-----------------------------|-----------------------------|
| Lorem ipsum dolor sit amet. | Lorem ipsum dolor sit amet. |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With