3

I was wondering if there is a way to save a chart generated using rCharts to a file (as an image). In particular, I want to save a map generated using 'leaflet' to a file.

For example, a sample map can be generated using:

require(rCharts)
L1 <- Leaflet$new()
L1$set(width = 1600, height = 800)
L1$setView(c(0,0), 3)
L1 

This opens up a map in the browser. Now, I can right click on the map and select Save Image, but this saves only a single tile of the map. I want to save the whole map to file, and automate it using a bit of code, (rather than manually clicking and saving), because I need to run the process in a loop. Any suggestions?

J Harkness
  • 31
  • 2
  • You can save as [HTML](https://github.com/ramnathv/rCharts/issues/5). For saving as image [this discussion](https://github.com/ramnathv/rCharts/issues/132) on the rChart GitHub page might be helpful. – ROLO Aug 07 '13 at 13:05
  • If you are looking for static maps, the easier route would be to use ggmap, which is very flexible and works great. The screenshot route linked to by @ROLO, while useful requires some work in installing node.js, casper.js and many dependencies. The ggmap solution would be a lot easier. – Ramnath Aug 07 '13 at 16:30

2 Answers2

0

I don't know about rCharts, but you might be able to use the Leaflet.print plugin to print to a static file.

Josh
  • 3,385
  • 5
  • 23
  • 45
0

This is years old, but I came across it searching for a solution for myself. I ended up creating one based on some other things I saw.

You can do this, but it has to be separated out into a few steps:

  1. First, run your plot as you would in rCharts. Make sure the standalone option = TRUE
  2. Next, save the plot as a stand alone html file
  3. Next, use phantomjs and rasterize.js on your local system to render the html file you just created into an image file
  4. Lastly, insert your image file

For the code below, assume your rCharts plot is already created as 'myplot'

tmpHtmlFile <- tempfile(fileext = '.html')
tmpPngFile <- tempfile(fileext = '.png')
myplot$save(tmpHtmlFile)
renderCommand <- sprintf('phantomjs ./reference/rasterize.js %s %s', tmpHtmlFile, tmpPngFile)
system(renderCommand)

Then use tmpPngFile to insert into your document

RobR
  • 76
  • 2