简体   繁体   中英

How to embed external HTML in r distill rmd

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

Background : I would like to embed / incorporate an external html file within a Distill Rmd file. I don't want to show the link to the html in the document, but the content of the html file. Something like include_graphics only for html.

I wasn't able to find something via google search. I only found Include HTML files in R Markdown file? where OP writes, he never figured out how to do it :(

From that post I tried htmltools::includeHTML("file.html") which didn't work,
and also shiny::includeHTML("file.html") which didn't work,
and even (it didn't work):

includes: 
      in_header: file.html

With htmltools::includeHTML I at least got the error message Error loading script: https://cdn.jsdelivr.net/npm//vega@5?noext which I couldn't make sense of :(
Though I can't reproduce this error-message now (a couple of weeks later). Maybe it is because I included library(htmltools) . Though there still is no html embedded :(

However the Distill package apparently is able to do embed html, as the _footer.html is included correctly, but automatically (such that i wasn't able to find the command in the code).

Thanks a lot!

You might want to double check your R chunk arguments, does it have include=FALSE ? because then you would be calling the HTML file, but then telling it not to be included, here is my distill Rmd file and the output with the included HTML code.

Rmarkdown

---
title: "Untitled"
output: distill::distill_article
---

```{r setup}
knitr::opts_chunk$set(echo = FALSE)
library(htmltools)


htmltools::includeHTML("test2.html")
```

HTML: titled test2.html located in same directory as .Rmd

<html>

<head>

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

<div class="chunk" id="unnamed-chunk-1"><div class="rcode"><div class="source"><pre class="knitr r"    ><span class="hl kwd">summary</span><span class="hl std">(cars)</span>
</pre></div>
<div class="output"><pre class="knitr r">##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
</pre></div>
</div></div>

<p>You can also embed plots, for example:</p>

<div class="chunk" id="unnamed-chunk-2"><div class="rcode"><div class="source"><pre class="knitr r"    ><span class="hl kwd">plot</span><span class="hl std">(cars)</span>
</pre></div>
</div><div class="rimage default"><img src="figure/unnamed-chunk-2-1.png" title="plot of chunk     unnamed-chunk-2" alt="plot of chunk unnamed-chunk-2" class="plot" /></div></div>

</body>
</html>

output rendered to html

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM