4

我正在尝试在 Shiny 中获取 rPivotTable 的屏幕截图。我正在使用 htmlwidgets 中的 saveWidget() 保存 rpivottable。然后我想使用 webshot() 将 html 转换为 jpeg 图像。从 saveWidget 生成的 html 在 Firefox 中正确显示,但从 webshot 生成的图像是空白的。我附上了从 webshot 生成的图像。 myPivot.jpeg

下面是我正在使用的示例代码

library(rpivotTable)
library(htmlwidgets)
library(webshot)
mypivot <- rpivotTable(mtcars, rows = "cyl", cols = "mpg")

saveWidget(mypivot, "myPivot.html", selfcontained = FALSE)

webshot(url = "myPivot.html", file = "myPivot.jpeg")

然后我尝试使用如何在 RStudio 地图中将 Leaflet 保存为 png 或 jpg 文件中的一种解决方案? 我仍然得到空白图像作为输出。下面是生成的图像(它完全是空白的白色) RPlot.png

下面是我正在使用的代码。

library(leaflet)
library(htmlwidgets)
library(webshot)

## create map
m <- leaflet() %>% addTiles()

## save html to png
saveWidget(m, "temp.html", selfcontained = FALSE)
webshot("temp.html", file = "Rplot.png",
        cliprect = "viewport")

以下是关于我的 R 会话的信息。我正在使用 RStudio。

R> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[2] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    
attached base packages:
[2] stats     graphics  grDevices utils     datasets  methods   base     
other attached packages:
[2] rpivotTable_0.1.5.20 htmlwidgets_0.6      mapview_1.1.0        leaflet_1.0.1        webshot_0.3          htmltools_0.3.5     
loaded via a namespace (and not attached):
 [2] Rcpp_0.12.5         RColorBrewer_1.1-2  plyr_1.8.4          R.methodsS3_1.7.1   R.utils_2.3.0       viridis_0.3.4       base64enc_0.1-3    
 [8] iterators_1.0.8     tools_3.2.2         gdalUtils_2.0.1.7   digest_0.6.9        viridisLite_0.1.3   satellite_0.2.0     lattice_0.20-33    
[15] jsonlite_0.9.22     gtable_0.2.0        png_0.1-7           foreach_1.4.3       shiny_0.13.2        DBI_0.4-1           yaml_2.1.13        
[22] rgdal_1.1-10        parallel_3.2.2      gridExtra_2.2.1     dplyr_0.4.3         httr_1.1.0          raster_2.5-8        stats4_3.2.2       
[29] grid_3.2.2          R6_2.1.2            plotly_3.6.0        sp_1.2-3            latticeExtra_0.6-28 tidyr_0.4.1         ggplot2_2.1.0      
[36] magrittr_1.5        scales_0.4.0        codetools_0.2-14    assertthat_0.1      mime_0.4            xtable_1.8-2        colorspace_1.2-6   
[43] httpuv_1.3.3        munsell_0.4.3       R.oo_1.20.0
4

2 回答 2

2

您应该考虑在https://github.com/rstudio/webshot2上尝试 webshot2而不是使用 webshot。我准备了一篇博客文章,其中包含有关 webshot2 的各种详细信息。您可以从这里查看详细信息。另外,请参阅我关于 webshot 与 webshot2 相比的问题的详细回答。

我已经用 webshot2 复制了你的场景。考虑到 webshot2 中的文件扩展名必须是“png”或“pdf”;因此,它也被修改。

编码

library(rpivotTable)
library(htmlwidgets)
library(webshot2)
mypivot <- rpivotTable(mtcars, rows = "cyl", cols = "mpg")

saveWidget(mypivot, "myPivot.html", selfcontained = FALSE)

webshot(url = "myPivot.html", file = "myPivot.png")

输出文件 在此处输入图像描述

于 2020-05-18T15:49:59.737 回答
-1

迟来的回应!

以下对我有用:

library(rpivotTable)
library(htmlwidgets)

savedPivot <- "savedPivot.html"
rpivotTable(iris)   
saveWidget(rpivotTable(iris),file.path(normalizePath(dirname(savedPivot)),basename(savedPivot)))

(基于@malcook 对 SO here的回答)

于 2017-10-15T10:04:47.530 回答