If you want to include a standalone HTML
file, it is a very bad practice to include it with in_header, before_body or after_body or with cat(readLines(...))
.
Why is it a bad practice?
A standalone HTML
file is a simple text file with tags. A minimal HTML
file looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
<!-- page content -->
</body>
</html>
To be valid, an HTML
file must comply with many constraints. For instance, there can be only one <body>
element. Therefore, if you include a standalone HTML
document in another HTML
document, you get an HTML
file with two <body>
elements. So, it is an invalid HTML
file.
Such a file can be badly rendered in a browser (most of browsers try to "understand" it even it is invalid) or can crash it. So, you have to choose a solution that produce a valid HTML
file.
I see two options to render a valid HTML
file.
Use knitr
child document
See the documentation about child document here. I think this is the most adapted solution to your problem.
Include external HTML
file in an <iframe>
element
You can embed any external HTML
file in an <iframe>
element. Here's a reproducible example.
Assume that you have the following file named embedded_file.Rmd
---
title: "Embedded file"
output: html_document
---
This is the content of the embedded file.
Here's the content of main.Rmd
file:
---
title: "Include external html file"
output: html_document
---
```{r generate-external-report, include=FALSE}
rmarkdown::render('embedded_file.Rmd')
```
External `HTML` file can be included in an `<iframe>` element:
```{r, echo=FALSE}
htmltools::tags$iframe(title = "My embedded document", src = "embedded_file.html")
```
When you render main.Rmd
, you get an <iframe>
with your embedded file. You have to set the width and height of the <iframe>
to get a good looking <iframe>
.
html
file that you want to be included: is anhtml
fragment or anhtml
standalone file?