【问题标题】:How to save a highchart to PNG image in R?如何在R中将highchart保存为PNG图像?
【发布时间】:2021-05-17 21:16:20
【问题描述】:

如何在 R 中将 highchart (type = 'organization') 导出为 PNG 图像?

这是一个例子:

library(highcharter)
library(tidyverse)

highchart() %>%
  hc_chart(type = 'organization') %>%
  hc_add_series(
    data = list(
      list(from = 'A', to = 'A1'),
      list(from = 'A', to = 'A2')
    )) 

我尝试如下,但是导出的文件是空的:

library(highcharter)
library(tidyverse)

png("org.png")

highchart() %>%
  hc_chart(type = 'organization') %>%
  hc_add_series(
    data = list(
      list(from = 'A', to = 'A1'),
      list(from = 'A', to = 'A2')
    ))

dev.off()

【问题讨论】:

    标签: r plot highcharts save export


    【解决方案1】:

    您可以使用包webshot 来实现这一点。阅读更多关于Export highchart widget as figure in R的信息。

    编辑:答案适用于 OP,使用 webshot2,意在替换 webshot。

    library(highcharter)
    library(tidyverse)
    library(webshot)
    
    
    org <- highchart() %>%
      hc_chart(type = 'organization') %>%
      hc_add_series(
        data = list(
          list(from = 'A', to = 'A1'),
          list(from = 'A', to = 'A2')
        ))
    
    htmlwidgets::saveWidget(widget = org, file = "org.html")
    getwd()
    webshot(url = "org.html", 
            file = "org.png",
            delay=3) # delay will ensure that the whole plot appears in the image
    dev.off()
    

    【讨论】:

    • 它适用于 html 文件。 png 文件仍然是空的。对于保存 png 文件,您还有其他建议吗?
    • 这很奇怪。 Webshot 会拍摄 url 的快照,因此它应该可以工作。所以即使上面的例子也不适合你?我的意思是,使用示例数据?您可以尝试增加delay。延迟是"Time to wait before taking screenshot, in seconds. Sometimes a longer delay is needed for all assets to display properly."。您也可以尝试使用debug = TRUE 看看是否有任何问题。
    • 是的,这个例子对我有用。如果我使用该函数的标准延迟,则 png 将保存图像的缺失部分。
    • 你可以试试webshot2。祝你好运,当你发现问题时请告诉我!
    • 太棒了!别客气。然后我会将 webshot2 添加到答案中。
    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2018-07-17
    相关资源
    最近更新 更多