Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use xtable to print html table in R markdown file

I would like to print a nicer table with a data frame in R mark down file, and I know how to use kable to print it, but I'd like a little bit more control options for which I am experimenting with the xtable package. However, I am really stuck with a silly problem which is that it doesn't print the table but html code in the file. So for example, if I want to print a default data set cars I would do:

```{r}
library(xtable)
print(xtable(head(cars)), type = "html")
```

which gives me this piece of html code.

## <!-- html table generated in R 3.2.5 by xtable 1.8-2 package -->
## <!-- Wed May 25 19:06:33 2016 -->
## <table border=1>
## <tr> <th>  </th> <th> speed </th> <th> dist </th>  </tr>
##   <tr> <td align="right"> 1 </td> <td align="right"> 4.00 </td> <td align="right"> 2.00 </td> </tr>
##   <tr> <td align="right"> 2 </td> <td align="right"> 4.00 </td> <td align="right"> 10.00 </td> </tr>
##   <tr> <td align="right"> 3 </td> <td align="right"> 7.00 </td> <td align="right"> 4.00 </td> </tr>
##   <tr> <td align="right"> 4 </td> <td align="right"> 7.00 </td> <td align="right"> 22.00 </td> </tr>
##   <tr> <td align="right"> 5 </td> <td align="right"> 8.00 </td> <td align="right"> 16.00 </td> </tr>
##   <tr> <td align="right"> 6 </td> <td align="right"> 9.00 </td> <td align="right"> 10.00 </td> </tr>
##    </table>

But I want an html table instead of the html code being displayed. Does anybody have any suggestions?

like image 262
Psidom Avatar asked May 25 '16 23:05

Psidom


People also ask

What is Knitr :: Kable?

10.1 The function knitr::kable() The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.


1 Answers

Make sure to put 'asis' in quotes.

```{r, results='asis'}
library(xtable)
print(xtable(head(cars)), type = "html")
```

enter image description here

like image 151
Edward R. Mazurek Avatar answered Oct 06 '22 03:10

Edward R. Mazurek