Insert HTML tables in knitr documents in R

末鹿安然 提交于 2019-12-08 17:25:36

If you want to preserve the formatting (and also not bother with XML/HTML churning), you can use an <iframe> to embed your full HTML document in the knitr doc like this:

```{r echo=FALSE, results='asis'}
tmp <- URLencode(paste(readLines("/path/to/table.htm"), collapse="\n"))

cat('<iframe src="data:text/html;charset=utf-8,', tmp ,
    '" style="border: none; seamless:seamless; width: 800px; height: 200px"></iframe>')
```

It won't show up in the RStudio viewer but it will show up in a real browser:

You'll need to tweak width and height (I could/should have made height a bit less for this example), but you'll have your fully formatted/styled tables in your knitted document this way.

NOTE: this only works if knitting to HTML.

Here is my solution. Read the html into R and using xtable to output as html tables

```{r, echo=FALSE, return='asis'}
library(xtable)
u = "http://en.wikipedia.org/wiki/List_of_airlines_of_Malaysia"
tables = as.data.frame(readHTMLTable(u)[1])
print(xtable(tables),type='html',comment=FALSE)
```
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!