0

如何将 DiagrammeR::grViz() 的 htmlwidget 图表输出添加到 PowerPoint 幻灯片?我更愿意保留矢量图形并尽量减少手动操作。

下面的代码main.R将 DOT 图很好地呈现为htmlwidget

# ./code/digraph-test.dot 
digraph my_plot_name {
A->B->C;
}

# main.R
library(DiagrammeR)
digraph_test <- grViz("./code/digraph-test.dot")

我想将此输出添加到 PowerPoint 幻灯片中。我从这篇文章中改编了以下代码。

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" )

它产生以下错误:

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.]

似乎有些人在 PowerPoint 的 LiveWeb 加载项上取得了成功。我遇到了 ActiveX 问题,只找到了 hack 解决方案,并选择不追究。必须有一个简单的解决方案,对吧?

4

1 回答 1

3

正如@alistaire 所建议的那样,Rmarkdown 将是此图的更好解决方案,但如果您在 PowerPoint 中需要它,我建议使用包 webshot 将其作为 png 文件获取。

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" )
于 2017-03-20T12:20:31.840 回答