1

权威指南谈到将 HTML 片段添加到其他文档https://bookdown.org/yihui/rmarkdown/html-document.html#html-fragments

它还将“包含”称为高级定制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. 


这允许在模板引擎中包含“标题”和正文之前/之后的 HTML 子元素。

在 RNotebook 的中间,我怎么说“insert-html-file-here”(类似于 Latex \input{} 表示法)?

高级内联“包含”的文档在哪里?


所以,我得到了初步回应,并想报告它。

我添加了[封装问题,因此使用<pre>它来获取它的本质]:

    {r, child="testme.html", eval=TRUE}
    # 空块内容,这里只插入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 />

我必须添加它,<BR />这样它才能在模板中播放得很好。

这就是浏览器呈现的样子。

在此处输入图像描述

这是 Knit-HTML 呈现的内容:

在此处输入图像描述


它不允许 child=testme.html 的 URL... 所以它不能通过 http:// 远程运行???

这是渲染的输出。它可能不喜欢嵌套的 DIV?第二个<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>
4

2 回答 2

2

在 RNotebook(bookdown)的中间,您可以直接在 markdown 文档(.Rmd 文件)的正文中插入 HTML 代码

这个 HTML 插入应该从任何块中完成。它也不必用任何特殊标签声明。例如,以下降价代码片段

HTML TEST

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

只会输出:

HTML TEST
Here you go

上面是内联选项。现在,如您的问题所述,如果您想插入一些来自外部文件的 HTML 代码,您可以使用以下块将其插入到您的主 Markdown 文档中:

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

除了现在从外部文件 (HTML_test.html) 执行您的 HTML 代码之外,它会为您提供与以前相同的输出:

HTML TEST
Here you go
于 2020-10-25T12:33:37.890 回答
1

我在这里看到了一个 hack ,他们这样做是为了在他们的 .Rmd 中插入 HTML。

library(htmltools)

```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```
于 2020-10-26T10:09:55.003 回答