I am trying to change my workflow to simultaneously display and save my graphs in R.
So far, following the advice on two other issues ( 1 , 2 ), I have done the following:
- Clear all devices:
graphics.off(); dev.new()
- Create an instance of the device for my desired file type (most commonly used
pdf
) pdf("file.pdf")
. - Return to RStudio:
dev.set(which = dev.list()["RStudioGD"])
- Build Functions.
plot(1:10, 1:10)
etc. - Copy to device:
dev.copy(which = dev.list()["pdf"])
- Close device:
dev.off(which = dev.list()["pdf"])
This works fine for some time, but I noticed a problem: sometimes the settings of my graph window in RStudio are all narrowed, and this, in turn, is passed to my file. In particular, I always shorten / stretch a part of the RStudio workspace graph window to make my code / console more or less dominant (for example, to the point where it par("pin")
returns (11.27, .157).
To get around this, I tried to switch the order of operations, first write to the device, and then copy it to RStudio (i.e., switch steps 3 and 4, and then copy to RStudioGD
instead of the external device).
However, although the file was written as expected, nothing is displayed in RStudio, and no warnings are generated:
graphics.off(); dev.new()
pdf("womp.pdf")
plot(1:10, 1:10)
dev.copy(which = dev.list()["RStudioGD"])
invisible(dev.off(which = dev.list()["pdf"]))
Can't copy something from a device in RStudio?