将 R 中的 htmlwidget 添加到 PowerPoint 幻灯片
Add an htmlwidget from R to a PowerPoint Slide
如何将 DiagrammeR::grViz() 的 htmlwidget 图表输出添加到 PowerPoint 幻灯片?我更愿意保留矢量图形并尽量减少手动操作。
下面 main.R
中的代码将点图很好地呈现为 htmlwidget
# ./code/digraph-test.dot
digraph my_plot_name {
A->B->C;
}
# main.R
library(DiagrammeR)
digraph_test <- grViz("./code/digraph-test.dot")
我想将此输出添加到 PowerPoint 幻灯片中。我从 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" )
它产生以下错误:
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.]
似乎有些人在 LiveWeb add-in for PowerPoint 上取得了成功。我运行进入了ActiveX问题,只找到hack解决方案,选择不追究。必须有一个简单的解决方案,对吗?
正如@alistaire 所建议的,Rmarkdown 将是此图的更好解决方案,但如果您在 PowerPoint 中需要它,我建议使用 package 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" )
如何将 DiagrammeR::grViz() 的 htmlwidget 图表输出添加到 PowerPoint 幻灯片?我更愿意保留矢量图形并尽量减少手动操作。
下面 main.R
中的代码将点图很好地呈现为 htmlwidget
# ./code/digraph-test.dot
digraph my_plot_name {
A->B->C;
}
# main.R
library(DiagrammeR)
digraph_test <- grViz("./code/digraph-test.dot")
我想将此输出添加到 PowerPoint 幻灯片中。我从 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" )
它产生以下错误:
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.]
似乎有些人在 LiveWeb add-in for PowerPoint 上取得了成功。我运行进入了ActiveX问题,只找到hack解决方案,选择不追究。必须有一个简单的解决方案,对吗?
正如@alistaire 所建议的,Rmarkdown 将是此图的更好解决方案,但如果您在 PowerPoint 中需要它,我建议使用 package 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" )