在 R Markdown 文件中包含 HTML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36524238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include HTML files in R Markdown file?
提问by Mike Williamson
Quick Summary
快速总结
How do I place HTML files in placewithin an R Markdown file?
如何将HTML文件到位的R降价文件中?
Details
细节
I have created some nice animated choropleth maps via choroplethr.
我通过choropleth创建了一些不错的动画 choropleth 地图。
As the link demonstrates, the animated choropleths function via creating a set of PNG images, which are then rolled into an HTML file that cycles through the images, to show the animation. Works great, looks great.
如链接所示,动画 choropleths 的功能是通过创建一组 PNG 图像,然后将这些图像滚动到一个循环浏览图像的 HTML 文件中,以显示动画。效果很好,看起来很棒。
But now I want to embed / incorporate these pages within the .Rmd file, so that I have a holistic report including these animated choropleths, along with other work.
但现在我想在 .Rmd 文件中嵌入/合并这些页面,以便我有一个整体报告,包括这些动画等值线,以及其他工作。
It seems to me there should be an easy way to do an equivalent to
在我看来应该有一个简单的方法来做一个等价于
Links:
链接:
[please click here](http://this.is.where.you.will.go.html)
or
或者
Images:
图片:

The images path is precisely what I want: a reference that is "blown up" to put the information in place, instead of just as a link. How can I do this with a full HTML file instead of just an image? Is there any way?
图像路径正是我想要的:一个被“炸毁”的参考,以将信息放置到位,而不仅仅是作为链接。如何使用完整的 HTML 文件而不只是图像来执行此操作?有什么办法吗?
Explanation via Example
举例说明
Let's say my choropleth HTML file lives in my local path at './animations/demographics.html'
, and I have an R Markdown file like:
假设我的 choropleth HTML 文件位于我的本地路径中'./animations/demographics.html'
,并且我有一个 R Markdown 文件,例如:
---
title: 'Looking at the demographics issue'
author: "Mike"
date: "April 9th, 2016"
output:
html_document:
number_sections: no
toc: yes
toc_depth: 2
fontsize: 12pt
---
# Introduction
Here is some interesting stuff that I want to talk about. But first, let's review those earlier demographic maps we'd seen.
!
where I have assumed / pretended that !!
is the antecedent that will do precisely what I want: allow me to embed that HTML file in-line with the rest of the report.
在那里我假设/假装这!!
是前因,它将完全满足我的要求:允许我将该 HTML 文件嵌入到报告的其余部分中。
Updates
更新
Two updates. Most recently, I still could not get things to work, so I pushed it all up to a GitHub repository, in case anyone is willing to help me sort out the problem. Further details can be found at that repo's Readme file.
两个更新。最近,我仍然无法正常工作,因此我将其全部推送到GitHub 存储库,以防有人愿意帮助我解决问题。可以在该存储库的自述文件中找到更多详细信息。
It seems that being able to embed HTML into an R Markdown file would be incredibly useful, so I keep trying to sort it out.
似乎能够将 HTML 嵌入到 R Markdown 文件中会非常有用,所以我一直在努力解决这个问题。
(Older comments)
(旧评论)
As per some of the helpful suggestions, I tried and failed the following in the R Markdown file:
根据一些有用的建议,我在 R Markdown 文件中尝试并失败了以下内容:
Shiny method:
闪亮方法:
```{r showChoro1}
shiny::includeHTML("./animations/demographics.html")
```
(I also added runtime:Shiny
up in the YAML portion.)
(我也在runtime:Shiny
YAML 部分添加了。)
htmltools
method:
htmltools
方法:
```{r showChoro1}
htmltools::includeHTML("./animations/demographics.html")
```
(In this case, I made no changes to the YAML.)
(在这种情况下,我没有对 YAML 进行任何更改。)
In the former case (Shiny
), it did not work at all. In fact, including the HTML seemed to muck up the functionality of the document altogether, such that the runtime seemed perpetually not-fully-functional. (In short, while it appeared to load everything, the "loading" spindel never went away.)
在前一种情况 ( Shiny
) 中,它根本不起作用。事实上,包含 HTML 似乎完全破坏了文档的功能,因此运行时似乎永远不能完全发挥作用。(简而言之,虽然它似乎加载了所有内容,但“加载”主轴从未消失。)
In the latter case, nothing else got messed up, but it was a broken image. Strangely, there was a "choropleth player" ribbon at the top of the document which would work, it's just that none of the images would pop up.
在后一种情况下,没有其他任何问题,但它是一个破碎的图像。奇怪的是,文档顶部有一个“choropleth player”功能区,它可以工作,只是不会弹出任何图像。
For my own sanity, I also provided simple links, which worked fine.
为了我自己的理智,我还提供了简单的链接,效果很好。
[This link](./animations/demographics.html) worked without a problem, except that it is not embedded, as I would prefer.
So it is clearly a challenge with the embedding.
因此,嵌入显然是一个挑战。
采纳答案by chinsoon12
Here is a hack (probably inelegant)...idea is to directly insert HTML programmatically in Rmd and then render Rmd.
这是一个hack(可能不雅)......想法是直接在Rmd中以编程方式插入HTML,然后呈现Rmd。
temp.Rmd file:
temp.Rmd 文件:
---
title: "Introduction"
author: "chinsoon12"
date: "April 10, 2016"
output: html_document
---
<<insertHTML:[test.html]
etc, etc, etc
```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```
etc, etc, etc
test.html file:
test.html 文件:
<html>
<head>
<title>Title</title>
</head>
<body>
<p>This is an R HTML document. When you click the <b>Knit HTML</b> button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:</p>
<p>test test</p>
</body>
</html>
verbose code to replace Rmd code with HTML code and then render (can probably be shortened by a lot)
用 HTML 代码替换 Rmd 代码然后渲染的详细代码(可能可以缩短很多)
library(stringi)
subHtmlRender <- function(mdfile, htmlfile) {
#replace <<insertHTML:htmlfile with actual html code
#but without beginning white space
lines <- readLines(mdfile)
toSubcode <- paste0("<<insertHTML:[",htmlfile,"]")
location <- which(stri_detect_fixed(lines, toSubcode) )
htmllines <- stri_trim(readLines(htmlfile))
#render html doc
newRmdfile <- tempfile("temp", getwd(), ".Rmd")
newlines <- c(lines[1:(location-1)],
htmllines,
lines[min(location+1, length(lines)):length(lines)]) #be careful when insertHTML being last line in .Rmd file
write(newlines, newRmdfile)
rmarkdown::render(newRmdfile, "html_document")
shell(gsub(".Rmd",".html",basename(newRmdfile),fixed=T))
} #end subHtmlRender
subHtmlRender("temp.Rmd", "test.html")
EDIT: htmltools::includeHTML also works with the sample files that I provided. Is it because your particular html does not like UTF8-encoding?
编辑: htmltools::includeHTML 也适用于我提供的示例文件。是因为您的特定 html 不喜欢 UTF8 编码吗?
EDIT: taking @MikeWilliamson comments into feedback
编辑:将@MikeWilliamson 评论纳入反馈
I tried the following
我尝试了以下
- copied and pasted animated_choropleth.htmlinto a blank .Rmd
- remove references to cloudfare.com as I had access issues while rendering (see below)
- knit HTML
- put back those cloudfare weblinks
- put the graphs in the same folder as the rendered html
- open the HTML
- 复制并粘贴animated_choropleth.html到一个空白.Rmd
- 删除对 cloudfare.com 的引用,因为我在渲染时遇到了访问问题(见下文)
- 编织 HTML
- 放回那些 cloudfare 网络链接
- 将图形与呈现的 html 放在同一文件夹中
- 打开 HTML
I appear to get back the html but am not sure if the result is what you expect
我似乎找回了 html,但不确定结果是否符合您的预期
Are you also facing the same issue in pt 2? You might want to post the error message and ask for fixes :). This was my error message
您在第 2 点中是否也面临同样的问题?您可能想要发布错误消息并要求修复:)。这是我的错误信息
pandoc.exe: Failed to retrieve http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.1/css/bootstrap.min.css
FailedConnectionException2 "cdnjs.cloudflare.com" 80 False getAddrInfo: does not exist (error 11001)
Error: pandoc document conversion failed with error 61
回答by thbtmntgn
Did you try the includes:option in your YAML header?
您是否尝试过YAML 标头中的includes:选项?
https://rmarkdown.rstudio.com/html_document_format.html#includes
https://rmarkdown.rstudio.com/html_document_format.html#includes
But maybe you'll have the same problem I have: I'd like to include the HTML file in a specific section in my RMarkdown document, not in the header or before/after body.
但也许您会遇到与我相同的问题:我想将 HTML 文件包含在我的 RMarkdown 文档的特定部分中,而不是包含在标题中或正文之前/之后。
回答by user3749036
can try put this line in the Rmarkdown and then knit. (YAML header "output: html_document"; if "runtime: shiny" somehow it does not work)
可以尝试将此行放在 Rmarkdown 中然后编织。(YAML 标头“输出:html_document”;如果“运行时:闪亮”不知何故它不起作用)