将gvisGeoMap对象保存为png图片

4

如何将googleVis中的gvis对象保存为png格式? ggvis有export_png函数,但该函数无法用于googleVis。

我看到有人问过类似的问题,但真的没有其他方法吗?

test_data <- data.frame(count=c(1,2,5),group=c("Australia","Austria","China"))

p <- gvisGeoMap(test_data,locationvar='group',numvar='count',options=list(dataMode='regions',colors="['0x0000ff', '0xff0000']"))

plot(p)

也许这篇帖子可以帮助你。 - Anders Ellern Bilgrau
我尝试过了。正如我所说,我不认为它能够工作(至少在我尝试时没有成功,如果我错了请纠正我),因为那篇文章是关于使用ggvis的export_png,而不是googleVis。 - Jan Stanstrup
我发现这个问题在这里也被问到了,但没有答案:http://stackoverflow.com/questions/33546394/r-googlevis-to-png-file?rq=1 - Jan Stanstrup
走一条漫长的路,你可以创建一个非常简单的闪亮应用程序(只使用你的绘图),并使用webshot包中的appshot()函数进行网页截图。 - MLavoie
2个回答

1
有很多方法可以进行此操作,但结果难以预测。 以下是一些技巧:
  1. Xvfb, imagemagick and browser.

    You need to have all three of them installed. This won't work on Windows. I am assuming you have install xvfb and imagemagick. You start xvfb server in shell:-

    Xvfb :3 -screen 0 1024x768x24 &
    

    Now in R, you can print the file as html:

    print(p, file="p.html")
    

    Now a system call:

    system("DISPLAY=:3      firefox     g1.html  &")
    

    Now print the file using:

    system("DISPLAY=:3 import -window root p.png")
    

    You will get the file as p.png. You can use some other browser also like chrome.

  2. Using wkhtmltopdf package.

    After installing wkhtmltopdf and having it in your PATH, use system call:

    print(p, file="p.html")
    system("wkhtmltoimage --enable-plugins --javascript-delay 10000   p.html p.png")
    

    Results aren't predictable. Sometime flash doesn't work. Sometime it works for some platform. I couldn't reproduce like my first solution. (For OP, this solution worked. This is a cross platform solution.)

  3. As shiny app, phantomjs and webshot:

    Assuming you have printed the file that I have given, create a shiny app using following methods:

    mkdir app # Create App Directory
    # Create UI
    cat <<- EOF > app/ui.R
    library(shiny)
    
    shinyUI(fluidPage(
      titlePanel("Google Chart"),
      mainPanel(
        includeHTML("../g1.html")
      )
    ))    
    EOF
    # Create server
    cat <<- EOF > app/server.R
    library(shiny)
    shinyServer(function(input, output) {
    })
    EOF
    

    Now install phantomjs:

    npm install -g phantomjs
    

    In r:-

    install.packages("webshot")
    appshot("app", "p.png")
    

    You will see that you won't get flash charts because phantomjs now doesn't support flash. So this method too is limited. Only first method will work but it's not cross platform. But you can proceed with that approach in windows by using equivalent stuff.


谢谢!当然,我希望有更简单的方法,但如果在接下来的一周内没有新的解决方案出现,我将接受这个答案。到目前为止,我只是在浏览器中打开了图形并打印到 PDF。这也可以,并且对于有限数量的图形可能是最快的方法。 - Jan Stanstrup
@JanStanstrup 你觉得使用wkhtmltopdf包怎么样?对我来说,它无法与flash一起使用。但是有些人报告成功了。 - khrm
我一会儿尝试一下。 - Jan Stanstrup
我现在试过了,它(wkhtmltoimage)可以工作!只需要记得添加 wkhtmltoimage 到 PATH 或在系统调用中指向 exe 文件即可。但是我无法弄清楚如何获得高分辨率的图像。但是 wkhtmltopdf 也可以工作,所以至少可以得到那个然后裁剪/转换。虽然不是特别方便,但它可以工作。 - Jan Stanstrup
@JanStanstrup 欢迎。我已经更新了我的答复,包含了您的回复信息。 - khrm
嗨,我想知道这些方法是否也适用于 ggvis? - Mario GS

0

目前,webshot2 可用。我在 这里 发布了我的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接