Preview a Saved Png in an R Device Window

Preview a saved PNG in an R device window

You can import it and display it in R,

library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
grid::grid.raster(img)

How to display a PNG file in RStudio viewer pane?

Using the R package magick:

library(magick)
#> Linking to ImageMagick 7.0.10.26
#> Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp, x11
#> Disabled features: fftw, ghostscript
#> Using 4 threads
# Assuming png package is installed.
filename <- system.file("img", "Rlogo.png", package="png")
image_read(filename)

Created on 2020-08-28 by the reprex package (v0.3.0)

R Need to restart RStudio to view and save in a file using dev.copy() and dev.off()

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

displaying ggplot .png in RStudio viewer pane with correct dimensions

The resolution needs to match that of your device. Default dpi is 300 in ggsave, but monitors are typically 96 dpi:

dims <- dev.size(units = "in")
plt_path <- tempfile(fileext = ".png")
ggsave(plt_path, plt, width = dims[[1]], height = dims[[2]], units = "in",
dpi = 96)

viewer <- getOption("viewer")
viewer(plt_path)

Sample Image

Display and save the plot simultaneously in R, RStudio

I found the previous answers as being incomplete. Drilled down on how to display the plot in the default RStudio "Plots" window and save the plot in "png" at the same time.

In case that someday, someone might come across the same question, here is how is being done, line by line:

R:> 
R:> dev.list() # fresh start. no graphical devices available at this point
NULL
R:> dev.cur() # no current device at this point
null device
1
R:> dev.new() # open graphical devices
NULL
R:> dev.list() # list them
RStudioGD quartz_off_screen
2 3
R:> png("plot50.png") # open an offscreen device as png. New device should be number 4
R:> dev.list() # the list of all graphical devices includes the newly created device, number 4
RStudioGD quartz_off_screen quartz_off_screen
2 3 4
R:> dev.cur() # NOTE: the new created device(number 4) becomes "current" automatically,
quartz_off_screen # as soon as it has been created
4
R:> dev.set(which = 2) # switch back to device 2 used to display the plot in the default RStudio
# "Plots" window
RStudioGD
2
R:> dev.cur() # indeed, RstudioGD becomes the current device after the switch step from above
RStudioGD
2
R:> dev.list() # just a check on all available devices. device 4 still in the list after
# the switch
RStudioGD quartz_off_screen quartz_off_screen
2 3 4
R:> plot(c(1:100)) # plot an example. It will be displayed in "Plots" window of RStudio
R:> dev.list() # just a check on all the available devices
RStudioGD quartz_off_screen quartz_off_screen
2 3 4
R:> dev.copy(which = 4) # copies from current device(RStudioGD) to device 4. It automatically sets
quartz_off_screen # device 4 as current
4
R:> dev.cur() # indeed , device 4 is the current device
quartz_off_screen
4
R:> dev.off() # close device 4. IMPORTANT: AT THIS POINT the plot is saved as
RStudioGD # png("plot50.png") in the current working directory.
# Three actions takes place at this point, all at once:
# 1. closes device 4
# 2. save the plot as "plot50.png"
# 3. sets the dev.next() (which is RStudioGD) as the current device
2
R:> dev.cur() # RStudioGD becomes current as soon as device 4 has been closed down.
RStudioGD
2
R:>

Improving quality of RStudio graphics device (preview)

You could install RStudio >= v1.4 and the package {ragg} and then set the graphic device backend to AGG as described here.

Sample Image

This should make the preview quality better.

Note that this problem is platform dependent. I have the same poor quality of previews on my windows machine, while I have no such problems (even without {ragg}) on my mac.

How to save a plot as image on the disk?

There are two closely-related questions, and an answer for each.


1. An image will be generated in future in my script, how do I save it to disk?

To save a plot, you need to do the following:

  1. Open a device, using png(), bmp(), pdf() or similar
  2. Plot your model
  3. Close the device using dev.off()

Some example code for saving the plot to a png file:

fit <- lm(some ~ model)

png(filename="your/file/location/name.png")
plot(fit)
dev.off()

This is described in the (combined) help page for the graphical formats ?png, ?bmp, ?jpeg and ?tiff as well as in the separate help page for ?pdf.

Note however that the image might look different on disk to the same plot directly plotted to your screen, for example if you have resized the on-screen window.


Note that if your plot is made by either lattice or ggplot2 you have to explicitly print the plot. See this answer that explains this in more detail and also links to the R FAQ: ggplot's qplot does not execute on sourcing


2. I'm currently looking at a plot on my screen and I want to copy it 'as-is' to disk.

dev.print(pdf, 'filename.pdf')

This should copy the image perfectly, respecting any resizing you have done to the interactive window. You can, as in the first part of this answer, replace pdf with other filetypes such as png.

RStudio doesn't save picture

The best way to do this, pressing zoom button in RStudio, then copy that pic to paint (which works), and then save it.



Related Topics



Leave a reply



Submit