1

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.

enter image description here

This is what Knit-HTML renders:

enter image description here


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>
Phil
  • 7,287
  • 3
  • 36
  • 66
mshaffer
  • 959
  • 1
  • 9
  • 19

2 Answers2

3

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
Marc
  • 2,183
  • 2
  • 11
  • 16
  • Thanks for some clarity. There isn't syntax to add a HTML file? I can only add another Rmd file using the ```{r}``` syntax? – mshaffer Oct 25 '20 at 12:49
  • I just checked by adding an HTML file (instead of the initial Rmd) and it works very well! I edited my answer accordingly – Marc Oct 25 '20 at 12:54
  • so, short answer to your question: yes, the {r} syntax allows you to insert the content of any HTML file inline (I just checked it in a real bookdown and it works smoothly) – Marc Oct 25 '20 at 12:55
  • 1
    Can child="http://www.somewhere.com/testme.html"? That is a URL? – mshaffer Oct 25 '20 at 13:14
  • I reviewed variations of the `img` tag, none of them seem to work. – mshaffer Oct 25 '20 at 13:27
  • I checked your 2 separate points above. 1) the HTTP external call to some URL does not work. It has to be a local HTML file. 2) `````` tags do work for me. I just inserted ```test image``` in my test HTML external file and the corresponding image displays OK in my output bookdown document. – Marc Oct 25 '20 at 13:41
  • Nested `
    ` seems to be the problem. I have `` inside a `
    `
    – mshaffer Oct 25 '20 at 13:42
  • Just for reference/reproducibility, my HTML (minimum) test file: ```

    Here you go

    test image
    ```
    – Marc Oct 25 '20 at 13:43
  • as you can see above, nested `````` inside a ```
    ``` works for me
    – Marc Oct 25 '20 at 13:44
  • When I add as `before_body: testme.html` it works fine. One is just "inputing" as-is, the other is trying to parse it. – mshaffer Oct 25 '20 at 13:46
  • and `before_body` does allow remote. – mshaffer Oct 25 '20 at 13:47
  • http://md5.mshaffer.com/WSU_STATS419/stackoverflow/64522384/ has a fully-working example. `before_body` works as expected and can work remotely. `inline` fails. – mshaffer Oct 25 '20 at 13:52
  • It may be nested `
    ` elements independent of the `IMG`
    – mshaffer Oct 25 '20 at 13:57
  • 1
    It is almost like I want `{r, child="testme.html", eval=TRUE}` to be `{r, include="testme.html", eval=TRUE}` so it behaves like the YAML `before_body` include. It doesn't parse, just includes. – mshaffer Oct 25 '20 at 14:00
  • 1
    You may want to check another option: bringing external HTTP content through an ```iframe```, like `````` – Marc Oct 25 '20 at 14:38
1

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

library(htmltools)

```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```
Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27