在 R Markdown 文件中包含 HTML 个文件?

Include HTML files in R Markdown file?

快速总结

如何将 HTML 文件 放置 在 R Markdown 文件中?

详情

我通过 choroplethr 创建了一些不错的动画等值线图。

如 link 所示,动画等值线通过创建一组 PNG 图像发挥作用,然后将其卷入循环显示图像的 HTML 文件中,以显示动画。效果很好,看起来很棒。

但现在我想将这些页面嵌入/合并到 .Rmd 文件中,这样我就有了一个包含这些动画等值线以及其他工作的整体报告。

在我看来应该有一个简单的方法来做相当于

链接:

[please click here](http://this.is.where.you.will.go.html)

图片:

![cute cat image](http://because.that.is.what.we.need...another.cat.image.html)

图像路径正是我想要的:一个 "blown up" 的引用,用于放置信息,而不是 link。我怎样才能使用完整的 HTML 文件而不只是图像来执行此操作?有什么办法吗?

示例说明

假设我的 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.

!![demographics map]('./animations/demographics.html')

我假设/假装 !! 是将完全按照我的意愿行事的前提:请允许我将 HTML 文件嵌入到报告的其余部分中。

更新

两次更新。最近,我仍然无法正常工作,所以我将它全部推到 GitHub repository,以防有人愿意帮我解决问题。可以在该存储库的自述文件中找到更多详细信息。

似乎能够将 HTML 嵌入到 R Markdown 文件中会非常有用,所以我一直在尝试整理它。


(较早的评论)

根据一些有用的建议,我在 R Markdown 文件中尝试并失败了以下内容:

闪亮方法:

```{r showChoro1}
shiny::includeHTML("./animations/demographics.html")
```

(我还在 YAML 部分添加了 runtime:Shiny。)

htmltools方法:

```{r showChoro1}
htmltools::includeHTML("./animations/demographics.html")
```

(在这种情况下,我没有对 YAML 进行任何更改。)

在前一种情况下(Shiny),它根本不起作用。事实上,包含 HTML 似乎完全破坏了文档的功能,以至于运行时似乎永远无法正常运行。 (简而言之,虽然它似乎加载了所有内容,但 "loading" 主轴从未消失。)

在后一种情况下,没有其他东西被弄乱,但它是一个损坏的图像。奇怪的是,文档顶部有一个 "choropleth player" 功能区可以使用,只是 none 的图像会弹出。


为了我自己的理智,我还提供了简单的 links,效果很好。

[This link](./animations/demographics.html) worked without a problem, except that it is not embedded, as I would prefer.

所以嵌入显然是一个挑战。

这是一个 hack(可能不够优雅)...想法是以编程方式直接在 Rmd 中插入 HTML,然后渲染 Rmd。

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 文件:

<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>

用 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")

编辑:htmltools::includeHTML 也适用于我提供的示例文件。是因为您的特定 html 不喜欢 UTF8 编码吗?


编辑:将@MikeWilliamson 的评论纳入反馈

我尝试了以下

  1. 复制粘贴animated_choropleth.html到空白.Rmd
  2. 删除对 cloudfare.com 的引用,因为我在访问时遇到了问题 渲染(见下文)
  3. 针织HTML
  4. 放回那些 cloudfare 网络链接
  5. 将图形与渲染的图形放在同一文件夹中 html
  6. 打开HTML

我似乎找回了 html 但不确定结果是否如您所愿

您在第 2 部分中是否也面临同样的问题?您可能想要 post 错误消息并要求修复 :)。这是我的错误信息

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

可以尝试将此行放入 Rmarkdown 然后编织。 (YAML header "output: html_document";如果 "runtime: shiny" 不知何故不起作用)

您是否在 YAML header 中尝试了 includes: 选项?

https://rmarkdown.rstudio.com/html_document_format.html#includes

但也许您会遇到与我相同的问题:我想将 HTML 文件包含在我的 RMarkdown 文档的特定部分中,而不是 header 或 before/after body.