繁体   English   中英

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

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

权威指南谈到将 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. 


这允许在模板引擎中包含一个“header”和 before/after body HTML 子元素。

在 RNotebook 中间,如何说“在此处插入 html 文件”(类似于 Latex \input{} 表示法)?

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


所以,我得到了初步的回应,并想就此进行报告。

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


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

我创建了一个文件“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 的 URLS... 所以它不能通过 http://???

这是呈现的 output。 它可能不喜欢嵌套的 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>

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

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

HTML TEST

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

将只是 output:

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

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

HTML TEST
Here you go

在这里看到了一个 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