Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 596
PMaier Avatar asked May 18 '15 15:05

PMaier


1 Answers

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

like image 101
kohske Avatar answered Sep 19 '22 14:09

kohske