Save viewer object rendered in RStudio as image

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.

It appears that this question has been asked before here, but the question was unanswered. How to automate "Save as image" in Viewer of 3d plot?

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")

Is this what you need?

library(plotly)
library(htmlwidgets)
library(webshot)
df <- mtcars
x <- plot_ly() %>%
  add_markers(data=mtcars, x=~wt, y=~disp)
x
saveWidget(x, "temp.html")
webshot("temp.html", "temp.png")
1 Like

No, I'm trying to use the RStudio built-in functionality rather than any package. RStudio can do this, but it's a manual button-clicking process. I can't find how to replicate the export as an image function using code.

So you want to trigger RStudio's own functionality programmatically? I've never heard of that before, I perhaps it is possible.

This person also used webshot:

So you want to trigger RStudio's own functionality programmatically?

Exactly. Any idea how this could be done? I figured posting this question on the RStudio forum would improve the odds of finding the answer. I've used webshot and orca to create images, but they change the layout of the chart slightly. I'd like it exported exactly as in the viewer, which I have sized specifically.

Unfortunately, RStudio does not provide a way for users to invoke the Save as Image command (or its equivalent underlying functionality). Would you mind filing this as a feature request at https://github.com/rstudio/rstudio/issues?

Sure, I'll do that. Thanks for your help.

Is it possible that the reason the webshot exported image is slightly different because the size of the viewport is different? You can set the size of the viewport in webshot to match that of the viewer pane in RStudio...

library(plotly)
library(htmlwidgets)
library(webshot)
df <- mtcars
x <- plot_ly() %>%
  add_markers(data=mtcars, x=~wt, y=~disp)
x
saveWidget(x, "temp.html")
webshot("temp.html", "temp.png", vwidth = 441, vheight = 351)
2 Likes

To clarify... htmlwidgets often auto-adapt to the size of the window they're being displayed in... webshot by default uses a window that is vwidth = 992 and vheight = 744, which is significantly larger than what the viewer pane in RStudio typically is, so that could be the reason that the exported image looks different than what you see in the viewer pane.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.