I want to add a flowchart (graph) to an Rmarkdown document I am creating. The flowchart needs to include numbers that are calculated by some R code.
I have installed both the DiagrammeR
and nomnoml
packages as they look like either of them might be able to do what I'm after. However, an attempt to knit the .Rmd document returns an error:
Error: Functions that produce HTML output found in document targeting docx 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: true
Note however that the HTML output will not be visible in non-HTML formats.
Execution halted
A sample .Rmd document:
---
title: "Untitled"
author: "Unauthored"
date: "n.d."
output: word_document
---
A DiagrammeR graph:
```{r}
DiagrammeR::grViz("digraph rmarkdown {A -> B}")
```
A nomnoml graph:
```{r}
nomnoml::nomnoml("[Hello]-[World!]")
```
Some writing after.
Short version: Installing webshot and PhantomJS enables the document to knit correctly. That can be achieved with:
install.packages("webshot")
webshot::install_phantomjs()
Slightly longer version: If you are using nomnoml
, as in this example, you will probably already have webshot
installed, as it is listed as an Imports
. In this case, you will only need to run the webshot::install_phantomjs()
command.
If you follow the instructions in the knitting error message adding the always_allow_html: true
command to the YAML frontmatter, and already have webshot installed but no PhantomJS, then the document will knit but without the flowcharts included. However, the output in the first code chunk does then have a helpful error message within the Word document that's output:
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Following that instruction, i.e., installing PhantomJS, and then re-knitting, produces the expected output. Both the DiagrammeR
graph and the nomnoml
graph are produced and inserted into the Word document.
You can also safely remove the always_allow_html: true
from the YAML frontmatter at this point, if you added it.