简体   繁体   中英

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

It also speaks of "includes" as 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.

In the middle of the RNotebook, how do I say "insert-html-file-here" (similar to the Latex \input{} notation)?

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


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

I created a file "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.

This is what a browser renders it as.

在此处输入图像描述

This is what Knit-HTML renders:

在此处输入图像描述


It does not allow URLS for the child=testme.html... so it cannot be run remotely over http://???

This is the output rendered. It may not like nested DIVs? The second <BR /> gets wrapped in a <p> tag, the first does not.

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

This HTML insertion should be done out of any chunks. It does not have to be declared with any special tag either. For example, the following markdown snippet

HTML TEST

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

will just 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:

```'{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):

HTML TEST
Here you go

I saw a hack here where they did this to insert HTML in their.Rmd.

library(htmltools)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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