Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xtable thead in html output

Tags:

html

r

xtable

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?

like image 574
bytesinflight Avatar asked Sep 17 '14 08:09

bytesinflight


1 Answers

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. |
like image 145
cdeterman Avatar answered Oct 13 '22 09:10

cdeterman