2

I am trying to create a plot and eventually save it as a file. But because I am making a lot of changes and want to test it out, I want to be able to view and save the plot at the same time. I have looked at this page to do what I want to do but in my system, it does not seem to be working as it is supposed to.

Here are my codes:

png('Save.png')

sample.df <- data.frame(group =  c('A','B','A','C','B','A','A','C','B','C','C','C','B'),
X = c(2,11,3,4,1,6,3,7,5,9,10,2,8),
Y = c(3,8,5,2,7,9,3,6,6,1,3,4,10))

plot(Y ~ X, data = sample.df)

dev.copy(png, 'Save.png')
dev.off()

There are several issues (I am new to R so I might be missing something entirely):

(1) When I use png(), I cannot view the plot in RStudio so I used dev.copy() but it does not allow me to view my plot in R studio

(2) Even after I use dev.off(), I cannot view the saved file until I close the RStudio (says "Windows Photo Viewer can't open this picture because the picture is being edited in another program"). I need to restart every time so it is very inconvenient.

What am I doing wrong and how could I view and view saved file without restarting RStudio every time? Thank you in advance!

Addition

Based on Love Tätting's comments, when I run dev.list(), this is what I get.

> png('Save.png')
> 
> sample.df <- data.frame(group =  c('A','B','A','C','B','A','A','C','B','C','C','C','B'),
+                         X = c(2,11,3,4,1,6,3,7,5,9,10,2,8),
+                         Y = c(3,8,5,2,7,9,3,6,6,1,3,4,10))
> 
> plot(Y ~ X, data = sample.df)
> 
> dev.copy(png, 'Save.png')
png 
  3 
> dev.off()
png 
  2 
> dev.list()
png 
  2 
> dev.off()
null device 
          1 
> dev.list()
NULL

Why do I not get RStudioGD?

owl
  • 1,841
  • 6
  • 20
  • 30

2 Answers2

5

RStudio has its own device, "RStudioGD". You can see it with dev.list(), where it by default is the first and only one.

R's design for decoupling rendering and backend is by the abstraction of devices. Which ones you can use is platform and environment dependent. dev.list() shows the stack of current devices.

If I understand your problem correctly you want to display the graph first in RStudio, and then decide whether you want to save it or not. Depending on how often you save th image you could use the 'export' button in the plot pane in RStudio and save it manually.

Otherwise, your choice of trying to copy it would be the obvious one for me as well.

To my knowledge the device abstraction in R does not allow one to encapsulate the device as an object, so one for example could make it an argument to a function that does the actual plot. Since dev.set() takes an index as argument, passing the index as argument will be dependent on state of the stack of devices.

I have not come up with a clean solution to this myself and have sometimes retorted to bracketing the plot rendering code with a call to a certain device and saving it right after, and switching device depending on a global.

So, if you can, use RStudios export functionality, otherwise an abstraction would need to maintain the state of the global stack of devices and do extensive testing of its state as it is global and you cannot direct a plot call to a certain device, it simply plots to the current device (to my knowledge).


Edit after OP comment

It seems that it is somewhat different behaviour you are experiencing if you cannot watch the file after dev.off, but also need to quit RStudio. For some type of plot frameworks there is a need to call print on the graphical object to have it actually print to the file. Perhaps this is done by RStudio at shutdown as part of normal teardown procedures of open devices? In that ase the file should be empty if you forcibly look in its contents before quiting RStudio. The other thing that sometimes work is to call dev.off twice. I don't know exactly why, but sometimes more devices get created than I have anticipated. After you have done dev.off, what does dev.list show?


Edit after OP's edit

I can see that you do, png(); dev.copy(); dev.off(). This will leave you with one more device opened than closed. You will still have the first graphics device that you started open as can be seen when you do the listing. You can simply remove dev.copy(). The image will be saved on dev.off() and should be able to open from the filesystem.

As to why you cannot see the RStudio graphics device, I am not entirely sure. It might be that other code is messing with your device stack. I would check in a clean session if it is there to make sure other code isn't tampering with the device stack. From RStudio forums and other SO questions there seem to have been plot pane related problems in RStudio that have resolved after updating RStudio to the latest. If that is a viable solution for you I would try that.

Love Tätting
  • 221
  • 2
  • 6
  • Thank you for your response. I know how to export the figure manually but the problem I am having is that if you do png('Save.png'), the figure does not display in RStudio (issue 1). So I cannot view the figure until I close RStudio and because I cannot view the Save.png file even after I run dev.off() for some reason, it has been really inconvenient (issue 2). – owl May 06 '17 at 07:46
  • Thank you for comments. As you say, after doing dev.off() twice, it did let me see the file before quitting RStudio. The only time I could see the figure in RStudio is when I do not run png('Save.png') but I am not sure if that is how it is supposed to work in R. – owl May 07 '17 at 07:26
  • You can imagine that RStudio does an "RStudio(plotpane)" when you start RStudio, so your described behaviour seem correct. When you do "png()" you add another device and make it the current one, so RStudios device get bypassed. The global stack of devices is a tricky part of R and one of its not so functionally inspired solutions :-) – Love Tätting May 07 '17 at 07:34
  • I see. I am new to R so I am still not sure the issues I am experiencing are whether something I am not doing properly (probably most of the case) or whether it is because of how R(Studio) works. – owl May 08 '17 at 04:32
1

I've just added support for RStudio's RStudioGD device to the developer's version of R.devices package (I'm the author). This will allow you to do the following in RStudio:

library("R.devices")

sample.df <- data.frame(
  group =  c('A','B','A','C','B','A','A','C','B','C','C','C','B'),
  X = c(2,11,3,4,1,6,3,7,5,9,10,2,8),
  Y = c(3,8,5,2,7,9,3,6,6,1,3,4,10)
)

figs <- devEval(c("RStudioGD", "png"), name = "foo", { 
  plot(Y ~ X, data = sample.df)
})

You can specify any set of output target types, e.g. c("RStudioGD", "png", "pdf", "x11"). The devices that output to file will by default write the files in folder figures/ with filenames as <name>.<ext>, e.g. figures/foo.png in the above example.

The value of the call, figs, holds references to all figures produced, e.g. figs$png. You can open them directly from R using the operator !. For example:

> figs$png
[1] "figures/foo.png"
> !figs$png
[1] "figures/foo.png"

The latter call should show the PNG file using your system's PNG viewer.

Until I submit these updates to CRAN, you can install the developer's version (2.15.1.9000) as:

remotes::install_github("HenrikBengtsson/R.devices@develop")
HenrikB
  • 6,132
  • 31
  • 34
  • Thank you for introducing your package. I did not expect to receive a response from a package developer. I tried running your codes but I get "Error: Device type RStudioGD・is not known/supported on this operating system/platform." – owl May 08 '17 at 04:32
  • Woops, my bad; the installation instructions were wrong (not for the develop version as intended). I've updated my answer above with the correct installation instructions. Please try again in a fresh R session. `packageVersion("R.devices")` should give `2.15.1.9000`. – HenrikB May 08 '17 at 05:03
  • 1
    Thank you. Your package does what I wanted to do. One question: how do you specify the path where you want to save the file? For example, how do you save 'foo.png' file directly on your desktop? – owl May 09 '17 at 16:18
  • 1
    For example, `devOptions("*", path = "~/Desktop")`, cf. the 'R.devices overview' vignette.The `"*"` means "the default options for all graphics device types". You can override these defaults per graphics device types, e.g. `devOptions("png", path = "/path/to/my_pngs/")` and `devOptions("pdf", path = "/path/to/my_pdfs/")`. – HenrikB May 09 '17 at 18:37
  • 1
    Worked great! I would just do devOptions('*', path = getwd()) to keep all the files saved in the same place as for other files :) – owl May 10 '17 at 20:43