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

42

简要概述

如何在R Markdown文件中“就地”放置HTML文件?

详情

我使用choroplethr创建了一些漂亮的动态区域地图。

正如链接所示,通过创建一组PNG图像,动态区域地图可以显示动画效果。这些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)

图片路径正是我需要的:一个被“放大”的引用,以便将信息放置在指定位置,而不仅仅是作为链接。如果使用完整的HTML文件,该如何实现呢?有没有办法?

通过示例进行解释

假设我的区域分级地图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存储库中,以便任何人愿意帮助我解决问题。 更多细节可以在该repo的Readme文件中找到。

似乎能够将HTML嵌入到R Markdown文件中将非常有用,因此我一直在尝试解决它。


(旧评论)

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

Shiny方法:

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

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

htmltools方法:

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

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

在前一种情况(Shiny)中,根本不起作用。实际上,包含HTML似乎彻底破坏了文档的功能,以至于运行时似乎永远不完全正常。简而言之,虽然所有内容都已加载,但“加载”旋转器从未消失。

在后一种情况下,没有其他混乱,但是图片被损坏。奇怪的是,在文档顶部有一个“区域分布播放器”丝带可以使用,只是没有任何图像弹出。


为了保持自己的理智,我还提供了简单的链接,这些链接工作正常。

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

所以很明显,这是嵌入的一个挑战。


@chinsoon12 嗯,我试过了,但是没有成功。可能是我尝试的方式不对,但我做了类似于这里(http://www.html5rocks.com/en/tutorials/webcomponents/imports/)的事情,但也没有成功。 - Mike Williamson
1
在代码块中完成。shiny::includeHTML 可以满足你的需求。 - alistaire
@MikeWilliamson,迈克,你最终解决了这个问题吗?我也有类似的问题。 - RobertMyles
@RobertMc 不好意思,我从来没有做过。:( - Mike Williamson
3个回答

19

这里有一个(可能不太优雅的)方法...我们的想法是在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>

将Rmd代码替换为HTML代码,然后渲染的冗长代码(可能可以大大缩短)。
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. knit HTML
  4. 重新添加这些cloudfare weblinks
  5. 将图形放在与呈现的html相同的文件夹中
  6. 打开HTML

似乎我得到了html,但不确定结果是否符合您的期望

您是否也面临第二步的相同问题?您可能需要发布错误消息并要求修复 :)。这是我的错误消息

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

嗨 @chinsoon12 ,这肯定看起来应该能行,但我还是没有运气。 实际上,代码并不是任何秘密,所以我把它们全部推到了 GitHub 存储库中。 如果您可以检查我上面制作的存储库链接,那将非常棒。 否则,非常感谢您迄今为止的帮助! - Mike Williamson
嗨@chinsoon12,感谢您的跟进!我知道那个错误,它来自于bootstrap的问题,这是一种使HTML“形状”更加灵活(允许更大或更小的屏幕)的方法。我看到你说得对,我只需要调整生成的choroplethr HTML文件本身就可以了。我没有想到那可能是问题所在。一旦我解决了这个问题,我会跟进choroplethr库的人。再次感谢! - Mike Williamson
没问题!在编辑中描述错误和修复将对每个人都有帮助 :) - chinsoon12
2
你尝试过在 <iframe> 元素中包含 HTML 文件吗? - RLesur
@romles 我之前没有尝试过。如果你遇到了困难,可以提出问题。 - chinsoon12
<<insertHTML:[test.html] 不支持 MathJax,但是 htmltools::includeHTML("test.html") 支持它,但如何调整大小呢? - Rγσ ξηg Lιαη Ημ 雷欧

3

2
谢谢@thbtmntgn!这是一个不错的部分解决方案,但正如你所说,我想要HTML嵌入到文档中,而不仅仅是在body之前或之后。所以它对我的情况并不完全适用。 - Mike Williamson

-4

你可以尝试将这行代码放入R Markdown中,然后进行 knit。(YAML 头部“output: html_document”,如果使用“runtime: shiny”无法正常工作)


2
抱歉,我觉得你误解了我的意图。我不是在问如何呈现HTML或Shiny。我想知道如何将整个HTML文件/页面放置在R markdown文档中。我可以剪切和粘贴文件中的HTML语法,但那不太优雅,而且HTML文件本身是一个预生成的、过于庞大的乱码,就像从R Markdown生成的HTML一样。 - Mike Williamson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接