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

Mik*_*son 21 html markdown r r-markdown

快速摘要

如何将HTML文件到位的R降价文件中?

细节

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

如链接所示,动画等值线通过创建一组PNG图像来起作用,然后将其转换为循环显示图像的HTML文件,以显示动画.效果很好,看起来很棒.

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

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

链接:

[please click here](http://this.is.where.you.will.go.html)
Run Code Online (Sandbox Code Playgroud)

要么

图片:

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

图像路径正是我想要的:一个"炸毁"以将信息放在适当位置的引用,而不仅仅是作为链接.如何使用完整的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')
Run Code Online (Sandbox Code Playgroud)

我假设/假装!!的前提是我想做的事情:允许我将HTML文件嵌入到报告的其余部分.

更新

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

似乎能够将HTML嵌入到R Markdown文件中会非常有用,所以我一直在努力解决它.


(旧评论)

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

闪亮的方法:

```{r showChoro1}
shiny::includeHTML("./animations/demographics.html")
```
Run Code Online (Sandbox Code Playgroud)

(我也加入runtime:Shiny了YAML部分.)

htmltools 方法:

```{r showChoro1}
htmltools::includeHTML("./animations/demographics.html")
```
Run Code Online (Sandbox Code Playgroud)

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

在前一种情况(Shiny)中,它根本不起作用.事实上,包括HTML似乎完全破坏了文档的功能,因此运行时似乎永远不能完全正常运行.(简而言之,虽然它似乎装载了所有东西,但"装载"的旋转螺旋却永远不会消失.)

在后一种情况下,没有其他东西搞砸了,但它是一个破碎的图像.奇怪的是,文档顶部有一个"等值线播放器"功能区可以正常工作,只是没有任何图像弹出.


为了我自己的理智,我还提供了简单的链接,这很好.

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

因此,嵌入显然是一个挑战.

chi*_*n12 12

这是一个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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

详细代码用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")
Run Code Online (Sandbox Code Playgroud)

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


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

我尝试了以下内容

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

我似乎找回了html但不确定结果是否符合您的预期

您是否也在第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
Run Code Online (Sandbox Code Playgroud)

  • 您是否尝试在 &lt;iframe&gt; 元素中包含 HTML 文件? (2认同)