将HTML文件包含到RMarkdown文档中生成HTML文档。

时间:2021-10-27 06:03:47

I've been developing a RMarkdown document to generate HTML reports with knitr package.

我一直在开发一个RMarkdown文档,用knitr包生成HTML报告。

I succeed to include a HTML file containing with the includes: option in the YAML header.

我成功地包含了一个包含了包含:YAML标题中的选项的HTML文件。

The problem is that I can only choose between 3 options (https://rmarkdown.rstudio.com/html_document_format.html#includes):

问题是我只能在三个选项中进行选择(https://rmarkdown.rstudio.com/html_document_format.html#包含):

  • in_header:
  • in_header:
  • before_body:
  • before_body:
  • or after_body:
  • 或after_body:

I'd like to include this HTML file where I want in my RMarkdown document, like in a specific section for example.

我想在我的RMarkdown文档中包含这个HTML文件,比如在特定的部分中。

Do you know how I can do that?

你知道我怎么做吗?

I found a similar question but answers did not help me.

我发现了一个类似的问题,但答案并没有帮助我。

2 个解决方案

#1


2  

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(...)).

如果您想包含一个独立的HTML文件,那么将它包含在in_header、before_body或after_body或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:

一个独立的HTML文件是一个带有标签的简单文本文件。一个最小的HTML文件是这样的:

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

要有效,HTML文件必须遵守许多约束。例如,只有一个元素。因此,如果在另一个HTML文档中包含一个独立的HTML文档,就会得到一个包含两个元素的HTML文件。因此,它是一个无效的HTML文件。这样的文件可能在浏览器中被严重渲染(大多数浏览器试图“理解”它,即使它是无效的)或者可以崩溃它。因此,您必须选择一个生成有效HTML文件的解决方案。

I see two options to render a valid HTML file.

我看到了显示有效HTML文件的两个选项。

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.

可以将任何外部HTML文件嵌入到

Assume that you have the following file named embedded_file.Rmd

假设您有以下文件名为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:

这是main的内容。限制型心肌病文件:

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

当你渲染主要。Rmd,你会得到一个

#2


0  

Maybe this is an ugly hack but just create your html file and within your markdown, create a code chunk like this:

也许这是一个丑陋的黑客,但只要创建html文件,在你的markdown中,创建这样的代码块:

```{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.

也许这就是@Clock奴隶所指的。

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

既然我对这个问题有了一个低的投票,我至少要发表评论。正如@romles指出的,一个html文件只允许有一个body标签。使用readLines命令,您不应该读取完整的HTML文件,但是您可以读取段落或表等。这应该不是问题。举个小例子

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

#1


2  

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(...)).

如果您想包含一个独立的HTML文件,那么将它包含在in_header、before_body或after_body或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:

一个独立的HTML文件是一个带有标签的简单文本文件。一个最小的HTML文件是这样的:

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

要有效,HTML文件必须遵守许多约束。例如,只有一个元素。因此,如果在另一个HTML文档中包含一个独立的HTML文档,就会得到一个包含两个元素的HTML文件。因此,它是一个无效的HTML文件。这样的文件可能在浏览器中被严重渲染(大多数浏览器试图“理解”它,即使它是无效的)或者可以崩溃它。因此,您必须选择一个生成有效HTML文件的解决方案。

I see two options to render a valid HTML file.

我看到了显示有效HTML文件的两个选项。

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.

可以将任何外部HTML文件嵌入到

Assume that you have the following file named embedded_file.Rmd

假设您有以下文件名为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:

这是main的内容。限制型心肌病文件:

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

当你渲染主要。Rmd,你会得到一个

#2


0  

Maybe this is an ugly hack but just create your html file and within your markdown, create a code chunk like this:

也许这是一个丑陋的黑客,但只要创建html文件,在你的markdown中,创建这样的代码块:

```{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.

也许这就是@Clock奴隶所指的。

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

既然我对这个问题有了一个低的投票,我至少要发表评论。正如@romles指出的,一个html文件只允许有一个body标签。使用readLines命令,您不应该读取完整的HTML文件,但是您可以读取段落或表等。这应该不是问题。举个小例子

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