Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include HTML files in R Markdown file?

Quick Summary

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

Details

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

Links:

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

or

Images:

![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 './animations/demographics.html', and I have an R Markdown file like:

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

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.

Updates

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} shiny::includeHTML("./animations/demographics.html") ``` 

(I also added runtime:Shiny up in the YAML portion.)

htmltools method:

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

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

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

like image 390
Mike Williamson Avatar asked Apr 10 '16 00:04

Mike Williamson


People also ask

Can you embed HTML in Markdown?

Span-level HTML tags — e.g. <span> , <cite> , or <del> — can be used anywhere in a Markdown paragraph, list item, or header. If you want, you can even use HTML tags instead of Markdown formatting; e.g. if you'd prefer to use HTML <a> or <img> tags instead of Markdown's link or image syntax, go right ahead.

Can you write HTML in R Markdown?

An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code, like the document below. --- output: html_document --- This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.

How do I share HTML in R Markdown?

By default, RMarkdown rendered as HTML are self-contained. This means you can compile the document into HTML file and simply email it to a colleague. It should have all that is needed to render the document on your colleague's web browser.


Video Answer


1 Answers

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 ---  <<insertHTML:[test.html]  etc, etc, etc  ```{r, echo=FALSE} htmltools::includeHTML("test.html") ```  etc, etc, etc 

test.html file:

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

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

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 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 
like image 147
chinsoon12 Avatar answered Oct 11 '22 15:10

chinsoon12