4

How can I save a gvis object from googleVis to png? ggvis has export_png but that doesn't work for googleVis.

I saw several asking this but is there really no way?

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)
Jan Stanstrup
  • 1,152
  • 11
  • 28
  • Perhaps [this post](http://stackoverflow.com/questions/27193287/using-ggvis-in-rnw-with-knitr/27193693#27193693) can help you. – Anders Ellern Bilgrau Jun 09 '16 at 13:13
  • I tried that. As I said I don't think it can work (at least it didn't work when I tried, correct me if I am wrong) because that posts refers to export_png that works with ggvis. Not with googleVis. – Jan Stanstrup Jun 09 '16 at 14:02
  • I found it was also asked here without an answer: http://stackoverflow.com/questions/33546394/r-googlevis-to-png-file?rq=1 – Jan Stanstrup Jun 13 '16 at 08:21
  • taking the long road, you could create a very simple shiny app (just with your plot) and take a webshot (using appshot()) from the package webshot. – MLavoie Jun 14 '16 at 16:08

2 Answers2

1

There are many ways to proceed with this with unpredictable results. Some of the tricks:

  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.

khrm
  • 5,363
  • 1
  • 22
  • 26
  • Thanks! I was of course hoping there was a more straight forward way but I will accept this as the answer if nothing new pops up in the next week. So far I have just opened the plot in my browser and printed to pdf. That works too and is perhaps the fastest for a limited number of plots. – Jan Stanstrup Jun 16 '16 at 09:16
  • @JanStanstrup What about Using wkhtmltopdf package? Is it working for you? For me it doesn't work with flash. But some people have reported success. – khrm Jun 16 '16 at 09:18
  • I'll give it a try in a little while. – Jan Stanstrup Jun 16 '16 at 09:27
  • I tried it now and it works (wkhtmltoimage)! just need to remember to add wkhtmltoimage to PATH or point it to the exe in the system call. I couldn't figure out how to get a high resolution image though. But wkhtmltopdf works too so you can at least get that and crop/convert. Not terribly convenient but it works. – Jan Stanstrup Jun 17 '16 at 12:04
  • @JanStanstrup Welcome. I updated my answer with your reply. – khrm Jun 17 '16 at 12:26
  • Hi, I was wondering if any of these methods works also for ggvis? – Mario GS Aug 22 '16 at 14:35
0

As of now, webshot2 is available. I posted my solution here.

Patrick
  • 742
  • 7
  • 19