Kristofersen Kristofersen - 24 days ago 7
Markdown Question

Include Image from HTML in Markdown Presentation

I am creating a markdown presentation and am generating a graph with the library data.tree. When I generate the presentation I get this error

Error: Functions that produce HTML output found in document targeting beamer output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:

always_allow_html: yes

Note however that the HTML output will not be visible in non-HTML formats.


If I include the always_allow_html: yes I just get the text output of my code chunk, like the error says.

How do I generate my graphics using data.tree and have it show up in my Markdown presentation?

Here's basically what my code looks like. I have change/removed nodes to make it easier to see.

----
output: beamer_presentation
---

```{r}
library(data.tree)
Parent = Node$new("Parent Node")
Child = Parent$AddChild("Child Node")

SetNodeStyle(Tree, fontsize = "24")
plot(Parent)
```


I also considered saving the html and loading it in from a local directory, but I wasn't able to find a way to do that.

Edit: Can you explain the down vote so I can fix any issues with this post? I do not believe it's a trivial answer. I did not find any similar posts or answers via google.

Answer

You may have been downvoted because this code

library(data.tree)
Parent = Node$new("Parent Node")
Child = Parent$AddChild("Child Node")

SetNodeStyle(Tree, fontsize = "24")
plot(Tree)

does not run, even outside the context of a markdown presentation. This is because "Tree" has not been defined. The tree object you're trying to refer to as "Tree" is what you've named "Parent". For a reproducible example, try:

library(data.tree)
Tree = Node$new("Parent Node")
Child = Tree$AddChild("Child Node")

SetNodeStyle(Tree, fontsize = "24")
plot(Tree)

Now how to get that plot into a slide:

You're on the right track with

saving the html and loading it in from a local directory

except you need to actually export it as something TeX can understand, not just save it as html. In RStudio, if you run the code above and produce the plot independently (not as part of the presentation), you have the option to "Export" > "Save as Image" directly from the viewer pane.

Supposing you've saved the plot in your working directory as "treeplot.png", you can then insert it into your presentation like so:

---
title: "Trees"
output: beamer_presentation
---

## Slide 1

![](treeplot.png)

This technique for inserting an image from a file is covered in the RMarkdown Reference Guide