简体   繁体   English

使用 R、RStudio 编织到 HTML,如何“内联”外部 HTML 文档块?

[英]Using R, RStudio Knit to HTML, how to include "inline" an external HTML document chunk?

The Definitive guide speaks of adding HTML fragments to other documents https://bookdown.org/yihui/rmarkdown/html-document.html#html-fragments权威指南谈到将 HTML 片段添加到其他文档https://bookdown.org/yihui/rmarkdown/html-document.html#html-fragments

It also speaks of "includes" as advanced customization https://bookdown.org/yihui/rmarkdown/html-document.html#advanced-customization它还将“包含”称为高级定制https://bookdown.org/yihui/rmarkdown/html-document.html#advanced-customization

---
title: "Habits"
output:
  html_document:
    includes:
      in_header: header.html
      before_body: doc_prefix.html
      after_body: doc_suffix.html
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 


This allows one to include a "header" and before/after body HTML subelements in the templating engine.这允许在模板引擎中包含一个“header”和 before/after body HTML 子元素。

In the middle of the RNotebook, how do I say "insert-html-file-here" (similar to the Latex \input{} notation)?在 RNotebook 中间,如何说“在此处插入 html 文件”(类似于 Latex \input{} 表示法)?

Where is the documentation on advanced, inline "includes"?有关高级内联“包含”的文档在哪里?


So, I got an initial response, and wanted to report on it.所以,我得到了初步的回应,并想就此进行报告。

I added [encapsulation issue so using <pre> to get essence of it]:我添加了[封装问题,所以使用<pre>来获取它的本质]:


    {r, child="testme.html", eval=TRUE}
    # empty chunk content, only inserting all content of testme.html here

I created a file "testme.html"我创建了一个文件“testme.html”

<BR />
<TABLE border=1>
  <TR>
    <TH>Hello there</TH>
    <TH rowspan=2>How is it going?</TH>
  </TR>
  <TR>
    <TD>I am doing fine</TD>
  </TR>
  <TR bgcolor="red">
    <TD valign="top" align="center" colspan=2>
      <DIV style="border: 2px solid black">
        <IMG src="2020-08-24_13-15-36.png" />
        <DIV>Here is my Caption</DIV>
      </DIV>
    </TD>
  </TR>
</TABLE>
<BR />

I had to add the <BR /> so it would play nice back in the template.我必须添加<BR />这样它才能在模板中很好地播放。

This is what a browser renders it as.这就是浏览器呈现的样子。

在此处输入图像描述

This is what Knit-HTML renders:这是 Knit-HTML 呈现的内容:

在此处输入图像描述


It does not allow URLS for the child=testme.html... so it cannot be run remotely over http://???它不允许 child=testme.html 的 URLS... 所以它不能通过 http://???

This is the output rendered.这是呈现的 output。 It may not like nested DIVs?它可能不喜欢嵌套的 DIV? The second <BR /> gets wrapped in a <p> tag, the first does not.第二个<BR />被包裹在<p>标签中,第一个没有。

<BR />
<TABLE border="1">
<TR>
<TH>
Hello there
</TH>
<TH rowspan="2">
How is it going?
</TH>
</TR>
<TR>
<TD>
I am doing fine
</TD>
</TR>
<TR bgcolor="red">
<TD valign="top" align="center" colspan="2">
<DIV style="border: 2px solid black;">
<pre><code>    &lt;IMG src=&quot;2020-08-24_13-15-36.png&quot; /&gt;
    &lt;DIV&gt;Here is my Caption&lt;/DIV&gt;
  &lt;/DIV&gt;
&lt;/TD&gt;</code></pre>
</TR>
</TABLE>
<p><BR /></p>

In the middle of a RNotebook (bookdown), you can insert HTML code directly in the body of your markdown document (.Rmd file)在 RNotebook(bookdown)中间,您可以直接在 markdown 文档(.Rmd 文件)的正文中插入 HTML 代码

This HTML insertion should be done out of any chunks.这个 HTML 插入应该从任何块中完成。 It does not have to be declared with any special tag either.它也不必使用任何特殊标记声明。 For example, the following markdown snippet例如,以下 markdown 片段

HTML TEST

<div class="container-fluid">
  <h1>Here you go</h1>
</div>

will just output:将只是 output:

HTML TEST
Here you go

Above was the inline option.以上是内联选项。 Now, as stated in your question, if you want to insert some HTML code coming from an external file, you can insert it in your main markdown document using the following chunk:现在,如您的问题所述,如果您想插入一些来自外部文件的 HTML 代码,您可以使用以下块将其插入主 markdown 文档中:

```'{r, child="HTML_test.html", eval=TRUE}
# empty chunk content, only inserting all content of HTML_test.html here
\```

which gives you the same output as before, except that your HTML code is now executed from the external file (HTML_test.html):它为您提供与以前相同的 output,除了您的 HTML 代码现在是从外部文件 (HTML_test.html) 执行的:

HTML TEST
Here you go

I saw a hack here where they did this to insert HTML in their.Rmd.在这里看到了一个 hack,他们这样做是为了在他们的 .Rmd 中插入 HTML。

library(htmltools)

```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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