How can I add the htmlwidget diagram output from DiagrammeR::grViz() to a PowerPoint slide? I'd prefer to keep vector graphics and minimize manual touches.
The code in main.R
below renders the DOT diagram nicely as an htmlwidget
# ./code/digraph-test.dot
digraph my_plot_name {
A->B->C;
}
# main.R
library(DiagrammeR)
digraph_test <- grViz("./code/digraph-test.dot")
I'd like to add this output to a PowerPoint slide. I adapted the following code from this post.
library( ReporteRs )
require( ggplot2 )
mydoc = pptx( )
mydoc = addSlide( mydoc, slide.layout = "Title and Content" )
mydoc = addTitle( mydoc, "Plot examples" )
myplot = grViz("./code/digraph-test.dot")
# myplot = qplot(Sepal.Length, Petal.Length
# , data = iris, color = Species
# , size = Petal.Width, alpha = I(0.7))
mydoc = addPlot( mydoc, function( ) print( myplot ), vector.graphic=TRUE)
writeDoc( mydoc, file = "test plot.pptx" )
It produces the following error:
Error in .jcall(slide, "I", "add", dml.object) :
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; The markup in the document preceding the root element must be well-formed.]
It seems some people have success with the LiveWeb add-in for PowerPoint. I ran into the ActiveX issue, only found hack solutions, and chose not to pursue. There must be a straightforward solution, right?
as suggested by @alistaire, Rmarkdown would be a better solution for this graph but if you need it in PowerPoint, I suggest using package webshot to get it as a png file.
library(DiagrammeR)
library( ReporteRs )
mydoc = pptx()
mydoc = addSlide( mydoc, slide.layout = "Title and Content" )
mydoc = addTitle( mydoc, "Plot examples" )
# compute new size for image to fit in ppt shape ----
shape_dim <- dim(mydoc)$size
shape_height <- setNames( shape_dim["height"], NULL ) * 72
shape_width <- setNames( shape_dim["width"], NULL ) * 72
# reuse the shape dimensions in grViz call ----
digraph_test <- grViz("digraph-test.dot", width = shape_width, height = shape_height )
htmlwidgets::saveWidget(widget = digraph_test, file = "digraph.html", selfcontained = TRUE)
webshot::webshot(url = "digraph.html", selector = '.html-widget-static-bound', file= "digraph.png")
mydoc = addImage( mydoc, filename = "digraph.png" )
writeDoc( mydoc, file = "test plot.pptx" )