PMaier
PMaier

Reputation: 654

Using DiagrammeR in Word document (generated using rMarkdown)

I've been looking at the diagrammeR package (http://rich-iannone.github.io/DiagrammeR/) to generate diagrams in rMarkdown. This works great when rendering the documents in HTML; now the question I have is whether there is a possibility to output the document as MS Word document?

For example, consider this:

---
title: "Test"
author: "Test"
date: "Monday, May 18, 2015"
output: html_document
---

```{r, echo=FALSE, warning=FALSE}
if (!require("DiagrammeR")) library("DiagrammeR")
```

Check out this diagram:

```{r, echo=FALSE, results='asis'}
DiagrammeR::grViz("
      digraph rmarkdown {
      node [shape = box ]
      'A' -> 'B'
      }
      ")
```

Using HTML as output format works like a charm. But, when I switch to MS Word, all I get is:

Error: Functions that produce HTML output found in document targeting docx output.
Please change the output type of this document to HTML.

Any ideas would be appreciated.

Many thanks, Philipp

Upvotes: 10

Views: 2215

Answers (1)

kohske
kohske

Reputation: 66842

trelliscope is useful: https://github.com/tesseradata/trelliscope

After installing http://phantomjs.org/download.html, You can generate word doc file by:

---
title: "Test"
author: "Test"
date: "Monday, May 18, 2015"
output: word_document
---

```{r include=FALSE}
if (!require("DiagrammeR")) library("DiagrammeR")
library(trelliscope)
```


Check out this diagram:

```{r, include=FALSE}
p = DiagrammeR::grViz("
      digraph rmarkdown {
      node [shape = box ]
      'A' -> 'B'
      }
      ")
widgetThumbnail(p, paste0(getwd(), "/hoge.png"))
```

![](hoge.png)

Here is the screenshot. It looks perfect :)

enter image description here

Upvotes: 2

Related Questions