9

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

Jaap
  • 81,064
  • 34
  • 182
  • 193
PMaier
  • 592
  • 7
  • 19
  • 1
    I would suggest asking Richard directly - he's riannone on twitter. He is very helpful. – jalapic May 18 '15 at 15:42
  • 1
    Please post what you learn because this package could be useful for org charts, among many other possibilities. Thank you. – lawyeR May 18 '15 at 15:48

1 Answers1

2

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

kohske
  • 65,572
  • 8
  • 165
  • 155
  • eventually I found `thumb.R` is a part of tesseradata/trelliscope: https://github.com/tesseradata/trelliscope so we should do `library(trelliscope)` after `install_github("tesseradata/trelliscope")` – kohske Aug 19 '15 at 14:53
  • Your solution looks ideal. I installed `trelliscope` and tried you code in both windows 8 and ubuntu 14.10. In each case, (a) diagram was properly rendered by `grViz` - `p` was valid. (b) resulting `hoge.png` file had an error message `abort() at (no stack trace available)`. I am not sure if this is an issue with `DiagrammerR` or `trelliscope` or `rmarkdown`. Same thing happens when the `widgetThumbnail()` is executed from the `RStudio` terminal prompt. – Sue Jun 12 '16 at 00:33