在R的knitr文档中插入HTML表 [英] Insert HTML tables in knitr documents in R

查看:73
本文介绍了在R的knitr文档中插入HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多不同的HTML文件,这些文件包含要在R的knitr报表中合并的格式化表格.不幸的是,在将HTML文件加载到R并将knitr报表中的表格包含在内时,我遇到了一些问题.

I have a number of different HTML files that contain formatted tables which I would like to combine in a knitr report in R. Unfortunately, I have some issues in loading the HTML files into R and including the tables in my knitr report.

HTML文件是使用MS Excel和stargazer库中的另存为htm"功能创建的.它们可以在任何浏览器中完美显示.我的代码是:

The HTML files were created using the "save as htm" function in MS Excel and the stargazer library. They display flawlessly in any browser. My code is:

```{r, echo=FALSE, return='asis'}
library(XML)
overview.html <- htmlParse("overview.htm")
print(overview.html)
```

在控制台中打印"overview html"时,我得到了正确的html代码.但是,编织报表时,输出文档不包含我的代码,并且出现以下错误:

When printing "overview html" in the console I get the correct html code. However, when kniting the report the output document does not contain my code and I get the following error:

Warning message:
XML content does not seem to be XML: 'overview.htm' 

我已经尝试了上述方法的几种变体(使用htmlTreeParse,使用打印类型="html"选项等)无济于事.如果有人可以提出一种可行的方法,那将是很棒的.

I have tried several variations of the above (using htmlTreeParse, using the print type = "html" option etc.) to no avail. It would be great if someone could suggest a way how this might work.

推荐答案

如果您想保留格式(并且不打扰XML/HTML搅动),则可以使用<iframe>将完整的HTML文档嵌入到编织文档是这样的:

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>')
```

它不会显示在RStudio查看器中,但它将会显示在真实的浏览器中:

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

您需要调整widthheight(在这个示例中,我本可以使height减少一些),但是您将在编织文档中使用完全格式化/样式化的表方式.

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.

注意:这仅适用于编织为HTML的情况.

NOTE: this only works if knitting to HTML.

这篇关于在R的knitr文档中插入HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆