I've been developing a RMarkdown document to generate HTML reports with knitr package.
I succeed to include a HTML file containing with the includes: option in the YAML header.
The problem is that I can only choose between 3 options (https://rmarkdown.rstudio.com/html_document_format.html#includes):
I'd like to include this HTML file where I want in my RMarkdown document, like in a specific section for example.
Do you know how I can do that?
I found a similar question but answers did not help me.
Why not using htmltools
package? As in this similar answer
```{r, echo=FALSE}
htmltools::includeHTML("my_text.html")
```
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(...))
.
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.
knitr
child documentSee the documentation about child document here. I think this is the most adapted solution to your problem.
HTML
file in an <iframe>
elementYou 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>
.
Maybe this is an ugly hack but just create your html
file and within your markdown, create a code chunk like this:
```{r, results='asis'}
cat(readLines('my_text.html'))
```
Don't forget to have an empty line at the end, otherwise you get an ugly warning.
Maybe this is what @Clock Slave was referring to.
EDIT:
Since I got a downvote on this, I want to at least comment. As @romles pointed out, an html file is only allowed to have one body
tag. So with my readLines
command, you shouldn't read a complete HTML file, but you can for example read paragraphs or tables, etc. This shouldn't be a problem. So a little example could be
<h1>My test html file</h1>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>
<ul>
<li>bla</li>
<li>blubb</li>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With