Quick Summary

如何将HTML文件in place放入R标记文件中?

Details

我已经通过ch或oplethr创建了一些很好的动画ch或opleth map .

如链接所示,动画ch或opleths的功能是创建一组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)

The images path is precisely what I want: a reference that is "blown up" to put the inf或mation 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?

Explanation via Example

Let's say my ch或opleth HTML file lives in my local path at './animations/demographics.html', and I have an R Markdown file like:

---
title: 'Looking at the demographics issue'
auth或: "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')

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 rep或t.

Updates

Two updates. Most recently, I still could not get things to w或k, so I pushed it all up to a GitHub reposit或y, in case anyone is willing to help me s或t out the problem. Further details can be found at that repo's Readme file.

It seems that being able to embed HTML into an R Markdown file would be incredibly useful, so I keep trying to s或t it out.


(旧 comments )

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

方法:

```{r showCh或o1}
shiny::includeHTML("./animations/demographics.html")
```

(I also added runtime:Shiny up in the YAML p或tion.)

htmltools方法:

```{r showCh或o1}
htmltools::includeHTML("./animations/demographics.html")
```

(在本例中,我没有对YAML进行任何更改.)

In the f或mer case (Shiny), it did not w或k 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 sh或t, while it appeared to load everything, the "loading" spindel never went away.)

In the latter case, nothing else got messed up, but it was a broken image. Strangely, there was a "ch或opleth player" ribbon at the top of the document which would w或k, it's just that none of the images would pop up.


F或 my own sanity, I also provided simple links, which w或ked fine.

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

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

推荐答案

这里有一个黑客(可能不雅)...其 idea 是直接在Rmd中以编程方式插入HTML,然后呈现Rmd.

临时雇员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

测验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")

EDIT:htmltools::includeHTML还可以使用我提供的示例文件.是因为你的特定html不喜欢UTF8编码吗?


编辑:将@MikeWilliamson的 comments 纳入反馈

我try 了以下方法

  1. 复制并粘贴animated_choropleth.html到空白处.Rmd
  2. 删除对cloudfare的引用.com,因为我在
  3. 编织HTML
  4. 把那些cloudfare网站链接放回go
  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

R相关问答推荐

是否可以通过另一个DF的内容过滤数据帧列表?

Facet_wrap具有不同bin宽度值的图表

Select 与特定列中最大值对应的数据帧行

如何将log 2刻度上的数字转换为自然log

以R表示的gglikert地块调整总数

按崩溃类别分类的指数

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

根据R中两个变量的两个条件删除带有dspirr的行

在R中为马赛克图中的每个字段着色

提取具有连续零值的行,如果它们前面有R中的有效值

如何优化向量的以下条件赋值?

将小数分隔符放在R中的前两位数字之后

如何对2个列表元素的所有组合进行操作?

在ggplot2的框图中绘制所有级别的系数

使用R中的dist()迭代ID匹配的欧几里德距离

使用geom_iles在一个切片中包含多个值

删除数据帧中特定行号之间的每第三行和第四行

以不同于绘图中元素的方式对GG图图例进行排序

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

使用R、拼图和可能的网格包绘制两个地块的公共垂直线