Search code examples
rrstudioknitrr-markdownggvis

ggvis plots refusing to knit to Word


I'm attempting to KNIT rmarkdown to Word with ggvis, and getting the below error:

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

Below is reproducible code to try it out. I am using RStudio. If you comment the last line, it will work fine. If you don't, it will fail.

---
title: "TestWordOutput"
author: "John"
date: "August 20, 2015"
output: word_document
---

```{r}
library(ggvis)
summary(cars)

plot(cars)

ggvis(cars, ~speed, ~dist)
```

I have done some research, and the closest thing I've found is this post. However, the code in the answer doesn't work for me.

Any help would be greatly appreciated.


Solution

  • Read this: http://rpackages.ianhowson.com/cran/ggvis/man/export_png.html (or do ?export_png). Follow the instructions here to try to get those Vega/node.js utils to work (I couldn't now, but have in the past), then do something like:

    ---
    title: "TestWordOutput"
    author: "John"
    date: "August 20, 2015"
    output: word_document
    ---
    
    ```{r}
    library(ggvis)
    summary(cars)
    
    plot(cars)
    
    ggvis(cars, ~speed, ~dist) %>% export_png("mtcars.png")
    ```
    
    ![](mtcars.png)
    

    And it should work for you. But, I'd stick with ggplot2 graphs if your target is Microsoft Word. They won't be interactive in Word and you can make super-awesome looking ggplot2 charts.