1

I created an object that is viewed in the RStudio viewer, such as chart in plotly or highcharter or a map in leaflet. I would like to save that object as a png. There are a number of ways to do this. For example, there's this method that uses webshot. It's possible to use orca. There are several methods. The problem with all of these is that the output image is slightly different than the version seen in the viewer. This is a problem for me because I need both the interactive version of the chart as well as a static version and I don't want to have to create two versions and write all the code associated with producing both to make sure each one comes out just as I need.

However, RStudio has a built-in method that can be accessed by clicking "Export -> Save as Image" in the viewer pane. I would like to use this method because it preserves the layout exactly as seen in the viewer.

I tried using R's built-in plot saving method of png, plot and devoff, but that appears to only work for plot objects, not viewer objects.

There has to be a way to reproduce the physical steps of saving as an image in RStudio programmatically, right? Doing it manually would be really time-consuming.

Here's an sample chart in plotly that shows up in the viewer.

plot_ly(z = ~volcano, type = "surface")
hmhensen
  • 2,974
  • 3
  • 22
  • 43

0 Answers0