Mike Williamson Mike Williamson - 4 months ago 19
HTML Question

Include HTML files in R Markdown file?

Quick Summary

How do I place HTML files in place within an R Markdown file?


I have created some nice animated choropleth maps via choroplethr.

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.

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.

It seems to me there should be an easy way to do an equivalent to


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

Explanation via Example

Let's say my choropleth HTML file lives in my local path at
, and I have an R Markdown file like:

title: 'Looking at the demographics issue'
author: "Mike"
date: "April 9th, 2016"
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 report.


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.

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.

(Older comments)

As per some of the helpful suggestions, I tried and failed the following in the R Markdown file:

Shiny method:

```{r showChoro1}

(I also added
up in the YAML portion.)


```{r showChoro1}

(In this case, I made no changes to the YAML.)

In the former case (
), 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.)

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.

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.


Here is a hack (probably inelegant)...idea is to directly insert HTML programmatically in Rmd and then render Rmd.

temp.Rmd file:

title: "Introduction"
author: "chinsoon12"
date: "April 10, 2016"
output: html_document


etc, etc, etc

```{r, echo=FALSE}

etc, etc, etc

test.html file:




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


verbose code to replace Rmd code with HTML code and then render (can probably be shortened by a lot)

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)],
                  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")
} #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?

EDIT: taking @MikeWilliamson comments into feedback

I tried the following

  1. copied and pasted animated_choropleth.html into a blank .Rmd
  2. remove references to cloudfare.com as I had access issues while rendering (see below)
  3. knit HTML
  4. put back those cloudfare weblinks
  5. put the graphs in the same folder as the rendered html
  6. open the HTML

I appear to get back the html but am not sure if the result is what you expect

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

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